Wince SQLce 数据库连接的怪事

e_zhiwen 2012-09-02 07:15:18
先看代码:


DB d = new DB();

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

带条件的无法取到数据,不带条件的可以取到数据.有点不太清楚为什么?

//再次修改过了条件,用模拟的又可以出结果。为什么呢?


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;
}


此数据完全无错,可是当代入我在数据库验证过的SQL有1 条记录,在断点调试中就是取不到结果。请教下高手帮忙!
...全文
124 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfzhu 2013-01-14
  • 打赏
  • 举报
回复
表名称大小写问题,已实际名称要一致。
e_zhiwen 2012-09-02
  • 打赏
  • 举报
回复
e_zhiwen 2012-09-02
  • 打赏
  • 举报
回复
e_zhiwen 2012-09-02
  • 打赏
  • 举报
回复
http://blog.csdn.net/e_zhiwen/article/details/7935198
此文中包括了二个截图,一个是调试的结果一个是数据库的记录。

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧