标题:查询EXCEL包含部分NULL的字段,麻烦给位大大了,谢谢!
只看楼主
god_hz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-20
结帖率:100%
已结贴  问题点数:20 回复次数:5 
查询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
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:20 
奇怪啊 按理说已经设置IMEX=1 应该可以避免空值啊
试着将HDR设为NO,将第一行做为数据读取,而IMEX=1就表示根据前8行判断列的数据类型,如果有字符型数据,那么就强制混合数据转换为文本。保证第一行为字符型,能将列的数据类型强制设为字符型,那么列中出现什么类型的数据都不怕了。但是字段1貌似不是字符型 可以试着改变下
难道说还是这个样子Isnull(字段3,'')



[ 本帖最后由 wangnannan 于 2010-11-20 09:49 编辑 ]

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-11-20 09:47
god_hz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-20
得分:0 
我的字段数据里有的就是NULL,莫非IMEX=1可以把NULL转换?
2010-11-20 09:52
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:0 
恩 。。。楼主介意上传代码么 ?

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-11-20 09:57
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
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:0 
既然楼主要把数据显示在datagridview控间中,和不判断datagridview里面的数据,这样是不是要简单一些啊?!
2010-11-21 20:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-326299-1-1.html




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

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