SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=CASPER_NIRVANA\FARID;Initial Catalog=proje;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO projetablosu(basvuru_no)VALUES(@basvuru_no)";
cmd.Parameters.AddWithValue("basvuru_no", textBox1.Text);
cmd.ExecuteNonQuery();
答案 0 :(得分:4)
好吧,如果它没有将记录添加到数据库中,你很难说它有用吗,可以吗?
您缺少参数中的@
。而不是
cmd.Parameters.AddWithValue("basvuru_no", textBox1.Text);
使用
cmd.Parameters.Add("@basvuru_no", SqlDbType.VarChar).Value = textBox1.Text;
您还应该阅读Can we stop using AddWithValue() already?
另外,正如Artem在评论中写道,你应该丢弃你的一次性物品。正确的方法是使用using
语句:
using (var conn = new SqlConnection(@"Data Source=CASPER_NIRVANA\FARID;Initial Catalog=proje;Integrated Security=True"))
{
using (var cmd = new SqlCommand("INSERT INTO projetablosu(basvuru_no)VALUES(@basvuru_no)", conn))
{
cmd.Parameters.Add("@basvuru_no", SqlDbType.VarChar).Value = textBox1.Text;
cmd.ExecuteNonQuery();
}
}
另请注意,您可以使用构造函数传递您在代码中手动设置的所有属性,从而使代码更短,更易读。
答案 1 :(得分:-2)
SqlConnection conn = new SqlConnection(@"Data Source=CASPER_NIRVANA\FARID;Initial Catalog=proje;Integrated Security=True");
string query = "INSERT INTO projetablosu(basvuru_no)VALUES('"+textBox1.Text+"')";
SqlCommand cmd = new SqlCommand(query,conn);
cmd.ExecuteNonQuery();
//尝试此代码,这将肯定适用于你