简体   繁体   中英

Saving data from textbox to MS Access database

This is my first attempt at using a database, and I'm stuck here, not too sure what to do. Here is my code:

        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");
    }

I'm trying to have the user save his information to the database based on what the user inputs into the textbox.

The order of your parameters should match that of the columns specified in your INSERT statement, hence:

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM