| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1193 人关注过本帖
标题:查询EXCEL包含部分NULL的字段,麻烦给位大大了,谢谢!
取消只看楼主 加入收藏
god_hz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
查询EXCEL包含部分NULL的字段,麻烦给位大大了,谢谢!
大概表结构如下:
字段1,字段2,字段3
1          aaa        1324
2          bbb
3          ddd         1324
4         ccc          123
5         eee         
6         fff         1324
根据字段3查询,我是用VS2005做的一个WINFORM程序,根据文本框内输入的数据来查询,部分代码如下:
链接字符串  string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RecordFile + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
SQL语句:string strPhone = "select * from [" + tableNamesArray[0] + "] where cstr(字段3)='" + strFind + "'";

比如我的STRFIND是1324的时候,提示"NULL值使用无效",后来我改为:
string strPhone = "select * from [" + tableNamesArray[0] + "] where cstr(isnull(字段3,0))='" + strFind + "'";
提示”参数个数不对“,SQL里ISNULL用在可以吗?
搜索更多相关主题的帖子: EXCEL 字段 NULL 麻烦 查询 
2010-11-20 09:17
god_hz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-20
收藏
得分:0 
我的字段数据里有的就是NULL,莫非IMEX=1可以把NULL转换?
2010-11-20 09:52
god_hz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-20
收藏
得分:0 
回复 3楼 god_hz
private void btnOpenRecord_Click(object sender, EventArgs e)//打开EXCEL文件
        {

            
            string str = @":\C";
            ofdExcel.InitialDirectory = str;
            string filter = "EXCEL文件(*.xls)|*.xls";
            ofdExcel.Filter = filter;
           
            app=new Excel.Application();
            if (ofdExcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                RecordFile = ofdExcel.FileName;
                lblRecordName.Text = RecordFile.ToString();//得到打开文件的名称
                strTableNames = GetExcelTableNames(RecordFile);//得到工作表名称

                tableNamesArray = strTableNames.Split(',');
                int i = tableNamesArray.Length;
                cboField.Items.Clear();
                cboSheet.Items.Clear();
                for(int j=0;j<i;j++)
                    cboSheet.Items.Add(tableNamesArray[j]);
               
               
                //打开话单得到记录集
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RecordFile + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                OleDbConnection conn = new OleDbConnection(strConn);
                OleDbDataAdapter myCommand = null;
                DataSet ds = new DataSet();
                try
                {
                    strExcel = "select * from [" + tableNamesArray[0] + "]";
                    conn.Open();
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds,"dsf");
                    dgvRecord.DataSource = ds.Tables["dsf"];
                    
                    //字段数和记录数显示到标签,读取字段名称
                    lblFieldNumber.Text=ds.Tables["dsf"].Columns.Count.ToString();
                    lblRecordNumber.Text = ds.Tables["dsf"].Rows.Count.ToString();
                    for (int j = 0; j < ds.Tables["dsf"].Columns.Count; j++)
                    {
                        cboField.Items.Add(dgvRecord.Columns[j].HeaderText);
                        if (dgvRecord.Columns[j].HeaderText == "飞")
                        {
                            isL = true;
                            isY = false;
                        }
                        if (dgvRecord.Columns[j].HeaderText == "飞1")
                        {
                            isY = true;
                            isL = false;
                        }
                    }

                    btnCountBase.Enabled = true;
                    btnCountPhone.Enabled = true;
                    btnViewt.Enabled = true;
                    btnViewOutput.Enabled = true;
                    //btnViewTime.Enabled = true;
                    
                    conn.Close();
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            else
            {
                return;
            }

        }     

//点击GRIDVIEW后查询
 private void dgvCountPhone_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            strFind = Convert.ToString(dgvCountPhone[0, dgvCountPhone.CurrentCell.RowIndex].Value).Trim();
            if (isBase && isL)//查看信息
            {
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RecordFile + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                try
                {

                    string strPhone = "select * from [" + tableNamesArray[0] + "] where  cstr(字段)='" + strFind + "'";
                    OleDbDataAdapter cmdCountPhone = new OleDbDataAdapter(strPhone, strConn);
                    DataSet dsPhone = new DataSet();
                    cmdCountPhone.Fill(dsPhone);
                    dgvRecord.DataSource = dsPhone.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
2010-11-21 08:52
快速回复:查询EXCEL包含部分NULL的字段,麻烦给位大大了,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035041 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved