C#SQL INSERT不会被保存

时间:2018-02-04 21:21:03

标签: c# sql sql-server database insert

我试图在mssql db中插入一个新行,但它不会被保存。谁能告诉我这是什么问题?

string connectionString = Properties.Settings.Default.DonationsConnectionString;

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionString);

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT Donations (Name, Betrag) VALUES ('" + donator.Text + "', '" + betrag.Text + "')";
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();

1 个答案:

答案 0 :(得分:0)

我想你忘了" INTO"插入sql语法

cmd.CommandText = "INSERT INTO Donations (Name, Betrag) VALUES ('" + donator.Text + "', '" + betrag.Text + "')";

但我建议你改变这个

cmd.CommandText = "INSERT INTO Donations (Name, Betrag) VALUES(@donator_temp,@betrag_temp);

cmd.Parameters.Add("@donator_temp", donator.text);
cmd.Parameters.Add("@betrag_temp", betrag.text);

cmd.ExecuteNonQuery();