插入文本框c#mdb数据库

时间:2015-04-20 05:08:40

标签: c# database insert

在数据库中插入代码时遇到问题,尝试将所有代码插入到相应的文本框中,但它无法正常工作。

我没有收到任何错误,但是sql代码无效。

  public partial class CreateAdmin : Form
    {
        OleDbConnection db = new OleDbConnection();
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        public CreateAdmin()
        {
            InitializeComponent();
            db.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ChattBankMDB.mdb";
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            db.Open();
            da = new OleDbDataAdapter("insert into Customers(CustID,CustPassword,CustFirstName,CustLastName,CustAddress,CustEmail)" + "VALUES (?,?,?,?,?,?)", db);
            db.Parameters.AddWithValue("@CustID", iDTextt.Text);
            db.Parameters.AddWithValue("@CustPassword", passText.Text);
            db.Parameters.AddWithValue("@CustFirstName", fnText.Text);
            db.Parameters.AddWithValue("@CustLastName", lnText.Text);
            db.Parameters.AddWithValue("@CustAddress", AddText.Text);
            db.Parameters.AddWithValue("@CustEmail", EmText.Text);
            db.ExecuteNonQuery();
        }

1 个答案:

答案 0 :(得分:0)

public partial class CreateAdmin : Form
    {
        OleDbConnection db = new OleDbConnection();
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        public CreateAdmin()
        {
            InitializeComponent();
            db.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ChattBankMDB.mdb";
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            db.Open();
         OleDbCommand   df = new OleDbCommand("insert into Customers(CustID,CustPassword,CustFirstName,CustLastName,CustAddress,CustEmail)" + "VALUES (?,?,?,?,?,?)", db);
            df.Parameters.AddWithValue("@CustID", iDTextt.Text);
            df.Parameters.AddWithValue("@CustPassword", passText.Text);
            df.Parameters.AddWithValue("@CustFirstName", fnText.Text);
            df.Parameters.AddWithValue("@CustLastName", lnText.Text);
            df.Parameters.AddWithValue("@CustAddress", AddText.Text);
            df.Parameters.AddWithValue("@CustEmail", EmText.Text);
            df.ExecuteNonQuery();
            db.Close();
        }