我试图在插入等时使用c#...当然很多人都建议我这样做。
但它一直在说错误:初始化字符串的格式不符合从索引0开始的规范。(翻译自丹麦语)
我的代码:
protected void BTN_reg_Click(object sender, EventArgs e)
{
string connectionString = "ConnectionString";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Users (Username, Password, Name, Role_id, Email, Sub, Acc) VALUES (@username, @pass, @name, @role, @email, @sub, @Acc);";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@username", TB_reg_username.Text);
cmd.Parameters.AddWithValue("@pass", TB_reg_pass.Text);
cmd.Parameters.AddWithValue("@name", TB_reg_name.Text);
cmd.Parameters.AddWithValue("@role", 2);
cmd.Parameters.AddWithValue("@email", TB_reg_email.Text);
cmd.Parameters.AddWithValue("@sub", CheckBox1.Checked);
cmd.Parameters.AddWithValue("@Acc", "False");
connection.Open();
cmd.ExecuteNonQuery();
}
}