本文介绍了在C#中插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在C#Windows应用程序的db中插入代码,这是我的代码:
i want to insert in db in C# windows application here is my code:
string str = "Data Source=|DataDirectory|\\Database1.sdf;Persist Security Info=False;";
Qconnection.ConnectionString = str;
Qcommand.Connection = Qconnection;
Qconnection.Open();
string commandText = "INSERT INTO order1 VALUES('10','20','o','u')";
Qcommand.CommandText = commandText;
Qcommand.CommandType = CommandType.Text;
// Qcommand.Parameters.AddWithValue("@RID",10 );
// Qcommand.Parameters.AddWithValue("@amount", orderColection.list[i].amount);
// Qcommand.Parameters.AddWithValue("@type", orderColection.list[i].type);
// Qcommand.Parameters.AddWithValue("@date", orderColection.list[i].date );
//Qcommand.ExecuteNonQuery();
Qcommand.ExecuteReader();
Qconnection.Close();
但是什么也不会发生.并且我的表为空
but nothing will happen. and my table is empty
推荐答案
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
{
com.Parameters.AddWithValue("@C1", myValueForColumn1);
com.Parameters.AddWithValue("@C2", myValueForColumn2);
com.ExecuteNonQuery();
}
}
如果使用的是SDF文件,则可能应该使用SqlCEConnections和SqlCECommands而不是上面的SqlConnection和SqlCommand.
If you are using SDF files, you probably should be using SqlCEConnections, and SqlCECommands rather than the SqlConnection and SqlCommand in the above though.
这篇关于在C#中插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!