数据将不会通过asp.net插入数据库
问题描述:
我试图插入一些标签有的文本,并且由于某种原因它不会插入它。数据将不会通过asp.net插入数据库
这是我的代码:
cmd = new SqlCommand(sqlquery1, conn);
cmd.Parameters.AddWithValue("Status", UserNameOrGuest.Text);
ErrorLabel.Text = "Movie rental succeeded!";
的sqlquery的是:string sqlquery1 = "INSERT INTO Movies (Status) VALUES (@Status)";
感谢您的帮助
答
您创建的SqlCommand
对象,但你没有执行命令来执行insert
操作。使用cmd.ExecuteNonQuery()
来执行该命令。
cmd = new SqlCommand(sqlquery1, conn);
cmd.Parameters.AddWithValue("Status", UserNameOrGuest.Text);
cmd.ExecuteNonQuery();
ErrorLabel.Text = "Movie rental succeeded!";
入住这里的例子:SqlCommand.ExecuteNonQuery Method和SqlCommand.ExecuteScalar Method
创建一个SqlCommand,然后使用的ExecuteNonQuery/ExecuteScaler执行。
+0
非常感谢:) – thormayer 2011-12-20 13:45:21
答
这是您的整个代码?你似乎错过了执行查询的调用?
cmd.ExecuteNonQuery();
答
您忘记了语法?
cmd.ExecuteNonQuery();
您是否收到错误? – UnhandledExcepSean 2011-12-20 13:27:06
你如何创建并打开连接?你什么时候调用cmd.ExecuteNonQuery?你能在你的问题中显示更多的代码吗? – 2011-12-20 13:27:11
那么'cmd.ExceuteNonQuery'在哪里?你会得到什么例外? – 2011-12-20 13:27:34