C#操作access数据库问题
程序代码:
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\db\numcity.mdb")) { conn.Open(); using (OleDbCommand cmd = new OleDbCommand()) { = "select * from numcity where id=1"; labnum.Text = Convert.ToString(cmd.ExecuteScalar()); } 提示:ExecuteScalar: Connection 属性尚未初始化。 为什么?如何解决?using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\db\numcity.mdb")) { conn.Open(); string sql = "select * from list where id=1"; using (OleDbCommand cmd = new OleDbCommand(sql,conn)) { labnum.Text = Convert.ToString(cmd.ExecuteScalar()); } } 这样就可以,和上面的有什么区别?
我刚学 各位高手指点啊!
刚才试了下,把
conn.Open();
string sql = null;
using (OleDbCommand cmd = new OleDbCommand(sql,conn))
{
labnum.Text = Convert.ToString(cmd.ExecuteScalar());
}
这样就可以,为什么?怎么用才规范?
[ 本帖最后由 孤零落叶寒 于 2011-4-4 19:40 编辑 ]