这是我第一次使用数据库,但是我被困在这里,不太确定该怎么做。这是我的代码:
private OleDbConnection connection = new OleDbConnection();
public AddProfile()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Caden\source\repos\PJBot\PJBot\bin\Debug\Profiles.mdb";
connection.Open();
//updateQuery.ExecuteNonQuery();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand updateQuery = new OleDbCommand("INSERT INTO Profiles (FullName,ProfileName,Email,Password,CardNumber,EXPMonth,EXPYear,CVV) VALUES(@name1,@name2,@email,@pass,@card,@expm,@expy,@cvv)", connection);
updateQuery.Parameters.Add("@name2", OleDbType.VarChar).Value = textBox1.Text; //Profile Name
updateQuery.Parameters.Add("@email", OleDbType.Numeric).Value = textBox2.Text; //Email
updateQuery.Parameters.Add("@pass", OleDbType.Numeric).Value = textBox3.Text; //Pass
updateQuery.Parameters.Add("@name1", OleDbType.VarChar).Value = textBox4.Text; //FullName
updateQuery.Parameters.Add("@card", OleDbType.VarChar).Value = textBox5.Text; //CardNumber
updateQuery.Parameters.Add("@expm", OleDbType.Numeric).Value = comboBox1.Text; //EXPMonth
updateQuery.Parameters.Add("@expy", OleDbType.Numeric).Value = comboBox2.Text; //EXPYear
updateQuery.Parameters.Add("@cvv", OleDbType.VarChar).Value = textBox7.Text; //CVV
updateQuery.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
}
我试图让用户根据用户在文本框中输入的内容将其信息保存到数据库中。
答案 0 :(得分:0)
参数的顺序应与INSERT
语句中指定的列的顺序相匹配,因此:
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand updateQuery = new OleDbCommand("INSERT INTO Profiles (FullName,ProfileName,Email,Password,CardNumber,EXPMonth,EXPYear,CVV) VALUES(@name1,@name2,@email,@pass,@card,@expm,@expy,@cvv)", connection);
updateQuery.Parameters.Add("@name1", OleDbType.VarChar).Value = textBox4.Text; //FullName
updateQuery.Parameters.Add("@name2", OleDbType.VarChar).Value = textBox1.Text; //Profile Name
updateQuery.Parameters.Add("@email", OleDbType.Numeric).Value = textBox2.Text; //Email
updateQuery.Parameters.Add("@pass", OleDbType.Numeric).Value = textBox3.Text; //Pass
updateQuery.Parameters.Add("@card", OleDbType.VarChar).Value = textBox5.Text; //CardNumber
updateQuery.Parameters.Add("@expm", OleDbType.Numeric).Value = comboBox1.Text; //EXPMonth
updateQuery.Parameters.Add("@expy", OleDbType.Numeric).Value = comboBox2.Text; //EXPYear
updateQuery.Parameters.Add("@cvv", OleDbType.VarChar).Value = textBox7.Text; //CVV
updateQuery.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
}