Asp.net Access2010更新问题C# codeprotected void btnTestInsert_Click(object sender, EventArgs e){usi
Asp.net Access2010更新问题
C# code protected void btnTestInsert_Click(object sender, EventArgs e) { using (OleDbConnection db = new OleDbConnection(myDB.Database.connstr)) { db.Open(); OleDbCommand cmd = new OleDbCommand("INSERT INTO [UserLeaveMess] ([UserId], [TheDate], [Messages]) VALUES (@p1, @p2, @p3)"); cmd.Connection = db; cmd.Parameters.Add("@p1", OleDbType.VarWChar, 50).Value = "Admin"; cmd.Parameters.Add("@p2", OleDbType.Date).Value = DateTime.Now; cmd.Parameters.Add("@p3", OleDbType.VarWChar).Value = txtMessage.Text.ToString(); try { cmd.ExecuteNonQuery();//报错,“对象无效或不再被设置。” } catch(Exception e1) { labError.Text = e1.Message; } } }
本人想搞个小的个人网站,就用Access数据库了,是2010版的,结果死活更新不了数据,蛋疼~·
查了相关资料,说有关权限设置问题,可以我删除怎么又可以呢,就是插入和修改不行。
删除的语句是这样写的,
OleDbCommand cmd = new OleDbCommand("Delete from [UserLeaveMess] where ID = @p1");
[解决办法]可能你的日期格式是不兼容的的
cmd.Parameters.Add("@p2", OleDbType.Date).Value = DateTime.Now;
你可以把TheDate字段在数据库中设置必填,默认值写Now(),然后就不要代码插入这个字段了。你或者这样
OleDbCommand cmd = new OleDbCommand("INSERT INTO [UserLeaveMess] ([UserId], [TheDate], [Messages]) VALUES (@p1, Now(), @p3)");
cmd.Connection = db;
cmd.Parameters.Add("@p1", OleDbType.VarWChar, 50).Value = "Admin";
cmd.Parameters.Add("@p3", OleDbType.VarWChar).Value = txtMessage.Text.ToString();
试试
[解决办法]你还可以写成
cmd.Parameters.Add("@p2", OleDbType.DBDate).Value = DateTime.Now;
参见
http://dotnet.aspx.cc/article/27c0d578-e4df-44dc-91a7-54ba10a53d1f/read.aspx