1

我的代码没有向数据库添加组合框值。这里可能有什么问题?

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();
}
4

1 回答 1

1

如果其 Web 应用程序考虑这一点:

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(); 
于 2013-10-09T17:48:27.640 回答