如何使用SqlCommand执行INSERT? [英] How do I execute an INSERT using SqlCommand?
问题描述
我有另一个创建连接的类(请参见精简类中途页面),我相信它连接或至少我没有在运行时遇到任何异常。
我现在想要将数据插入我的数据库,但不更新。
我试图关注这< a>示例,但它是用于阅读。
我在显示SQL窗格上运行了querystring,并且执行正确。它会将数据正确插入新行。
不知道在最后一行后做什么?感谢。
string queryString =
INSERT INTO UserData UserProfileID,ConfidenceLevel,LoveLevel,HappinessLevel)VALUES('a051fc1b- 4f51-485b-a07d-0f378528974e',2,2,2);;
protected void Button1_Click(object sender,EventArgs e)
{
MySqlConnection database = new MySqlConnection();
database.CreateConn();
Command = new SqlCommand(queryString,database.Connection); //
}
Command.ExecuteNonQuery();
顺便说一下,你的SQL查询字符串中缺少一个括号。
当然,你会在一个真正的查询中使用参数(你不会使用字符串连接,而不是传递参数,因为它会打开破坏性的SQL注入攻击的门户。)。 p>
I have another class that creates the connection (see refined class half way down page) and I believe it connects or at lest I do not hit any of the exceptions in run time.
I would now like to insert data into my database but nothing is updated. I am trying to follow this example but it is for reading.
I did run "querystring" on the show SQL Pane and it does execute correctly. It inserts data properly as a new row.
not sure what to do after my last line? Thanks.
string queryString =
"INSERT INTO UserData UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES ('a051fc1b-4f51-485b-a07d-0f378528974e', 2, 2, 2);";
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection database = new MySqlConnection();
database.CreateConn();
Command = new SqlCommand(queryString, database.Connection); //
}
Command.ExecuteNonQuery();
By the way, you're missing a parenthesis in your SQL query string.
And of course, you'd use parameters in a real query (and you will not use string concatenation instead of passing parameters as it'll open doors for destructive SQL injection attacks).
这篇关于如何使用SqlCommand执行INSERT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!