Wince SQLce 数据库连接的怪事

怪事呀,代码如下:


               DB d = new DB();
            
            //string sql = "Select BarcodeType,BarcodeContext From BarcodeTable Where Barcode='"+textBox1.Text.Trim()+"'";
            //string sql = "Select Barcode,Barcodetype,barcodecontext from barcodetable";
	    string sql="Select barcode,barcodetype,barcodecontext from barcodetable where barcode like '%"+textBox1.Text+"%'";
            DataTable dt1 = new DataTable();
            dt1 = d.SelectContext(sql);
            dataGrid1.DataSource = dt1;



代码中,加入了Where  就无法取到数据,删除条件后就可以取到二条记录。WHY?

再试着用了一个模糊条件查询发现可以取到数据了,但这个又想到一个问题,我的数据库中barcode字段是nvarchar(50)null属性的。

是不是用=来精确查询时barcode字段中存在了空格这样的问题呢?但nvarchar不是一个变长的字段吗?太奇怪了!


 public DataTable SelectContext(string sql)
        {
            SqlCeConnection conn=opensqlce();
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {

                SqlCeCommand cmd = new SqlCeCommand(sql, conn);
                SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

                da.Fill(ds);
                dt = ds.Tables[0];
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return dt;
           


        }
        private SqlCeConnection opensqlce()
        {
            string ceConnStr = @"Data Source ="+System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)+"\\TPM.sdf";
            SqlCeEngine engine = new SqlCeEngine(ceConnStr);
            SqlCeConnection connMobile = new SqlCeConnection(ceConnStr);
            try
            {
                connMobile.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("不能连接数据库!" + ex.Message);
                return null;
            }
            return connMobile;
        }

调试的结果如下,就是找不到正确的数据。

在数据库中的截图如下:


没有正确的数据返回,试过DataReader更是错误报hasRows出错,讲不支持此属性。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值