C#中如何向数据库中插入数据
How to insert data to a database in C#
我刚开始学习数据库,实际上我不知道为什么下面的代码不起作用。我搜索了很多,但我没有意识到到底发生了什么。
我有一个 Windows 表单应用程序,其中有一个 textbox
和一个 button
。当我点击按钮时,textbox
中的文本应该被插入到数据库中,但实际上什么也没有发生。
我用 Visual Studio 创建了数据库。我在 Visual Studio 2019 社区版的服务器资源管理器中右键单击 "Data connections",然后单击 "Create New SQL Server Database"。
using (SqlConnection con = new SqlConnection("Data Source = (localdb)\mssqllocaldb; Initial Catalog = myTestDB; Integrated Security = True; Pooling = False"))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSER INTO [tesTable] VALUES (@fname, @lname)", con);
cmd.Parameters.AddWithValue("@fname", textBox1.Text);
cmd.Parameters.AddWithValue("@lname", textBox2.Text);
}
MessageBox.Show("done!");
因为你还没有执行你的命令:
....
cmd.Parameters.AddWithValue("@lname", textBox2.Text);
cmd.ExecuteNonQuery();
您的 insert
语句的固定版本应该是这样的:
"INSERT INTO tesTable (fname,lname) VALUES (@fname,@lname)"
这里还需要补充一点,直接指定类型然后使用Value
属性比AddWithValue
:
更好
cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = textBox2.Text;
private void btnaddsave_Click(object sender, EventArgs e)
{
try
{
string connection1 = "server=localhost;user id=root;password=1234;database=library_system";
con = new MySqlConnection(connection1);
con.Open();
string q = "Insert Into library_system.add_book(Book_No,Book_Name)values('"+tbbno.Text+"','" + tbbn.Text+ "')";
cm1 = new MySqlCommand(q, con);
cm1.ExecuteNonQuery();
//con.Open();
MessageBox.Show("data saved", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
// load();
tbbno.Text = "";
tbbn.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我刚开始学习数据库,实际上我不知道为什么下面的代码不起作用。我搜索了很多,但我没有意识到到底发生了什么。
我有一个 Windows 表单应用程序,其中有一个 textbox
和一个 button
。当我点击按钮时,textbox
中的文本应该被插入到数据库中,但实际上什么也没有发生。
我用 Visual Studio 创建了数据库。我在 Visual Studio 2019 社区版的服务器资源管理器中右键单击 "Data connections",然后单击 "Create New SQL Server Database"。
using (SqlConnection con = new SqlConnection("Data Source = (localdb)\mssqllocaldb; Initial Catalog = myTestDB; Integrated Security = True; Pooling = False"))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSER INTO [tesTable] VALUES (@fname, @lname)", con);
cmd.Parameters.AddWithValue("@fname", textBox1.Text);
cmd.Parameters.AddWithValue("@lname", textBox2.Text);
}
MessageBox.Show("done!");
因为你还没有执行你的命令:
....
cmd.Parameters.AddWithValue("@lname", textBox2.Text);
cmd.ExecuteNonQuery();
您的 insert
语句的固定版本应该是这样的:
"INSERT INTO tesTable (fname,lname) VALUES (@fname,@lname)"
这里还需要补充一点,直接指定类型然后使用Value
属性比AddWithValue
:
cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = textBox2.Text;
private void btnaddsave_Click(object sender, EventArgs e)
{
try
{
string connection1 = "server=localhost;user id=root;password=1234;database=library_system";
con = new MySqlConnection(connection1);
con.Open();
string q = "Insert Into library_system.add_book(Book_No,Book_Name)values('"+tbbno.Text+"','" + tbbn.Text+ "')";
cm1 = new MySqlCommand(q, con);
cm1.ExecuteNonQuery();
//con.Open();
MessageBox.Show("data saved", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
// load();
tbbno.Text = "";
tbbn.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}