使用方法如下
public void opensql()
{
//SqlCommand cm = new System.Data.SqlClient.SqlCommand();
//SqlConnection cn = new SqlConnection();
SqlCeCommand cm = new SqlCeCommand();
SqlCeConnection cn = new SqlCeConnection();
//cn.ConnectionString = "Data Source=symbol.sdf";
//cn.ConnectionString = "server=JC91109;uid=sa;pwd=sa;database=symbol";
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
string dbcStr = @"Data Source =" + appPath.Replace(@"\\", @"\") + @"\symbol.sdf";
cn.ConnectionString = ""+dbcStr+"";
cn.Open();
cm.Connection = cn;
}
private void button1_Click(object sender, EventArgs e)
{
opensql();
cm.CommandText = "insert into scan(barcode) values('123')";
cm.ExecuteNonQuery();
SqlDataReader rs = cm.ExecuteReader();
}
在PDA中运行报错,在cm.CommandText = "insert into scan(barcode) values('123')";处显示没有NEW对象的实例。
在PDA的程序当前目录下有SYMBOL.SDF文件并有表SCAN.
注意cm局域变量的范围,原代码写得有点乱,楼主需要好好了解SQLConnection,SQLCommmand等基本概念,,并尽量使用参数化存储过程,以防止注入。建议了解SQLHelper组件
http://msdn.microsoft.com/zh-cn/library/system.data.sqlserverce.sqlceconnection%28VS.80%29.aspx