我的代码没有向数据库添加组合框值。这可能是什么问题?
private void Submit_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=CEX-PC\SQLEXPRESS;"
+ "Initial Catalog=inventorydatabase;"
+ "Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string query
= "INSERT INTO userdetail (username, password, position) VALUES ('"
+ username.Text + "', '" + password.Text + "', '"
+ position.SelectedValue + "' )";
SqlCommand command = new SqlCommand(query, con);
command.ExecuteNonQuery();
con.Close();
}
答案 0 :(得分:1)
如果其网络应用程序考虑到这一点:
string connectionString = @"Data Source=CEX-PC\SQLEXPRESS;Initial
Catalog=inventorydatabase;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string query = "INSERT INTO userdetail (username, password, position) VALUES(@username,@password,@val )";
SqlCommand command = new SqlCommand(query, con);
command.CommandType= CommandType.Text;
command.Parameters.AddWithValue("@username",txt1.text);
command.Parameters.AddWithValue("@password",txt2.text);
command.Parameters.AddWithValue("@val",ddl.SelectedItem.Value);
command.ExecuteNonQuery();
con.Close();