简体   繁体   English

无法从Visual C#将数据插入MS Access数据库

[英]Unable to insert data into MS Access database from Visual C#

just a beginner in Programing ... I use a class to handle the connectionstring . 只是编程的初学者。我用一个类来处理connectionstring。

public class DataBase { public OleDbConnection con = new OleDbConnection(); 公共类数据库(public OleDbConnection con = new OleDbConnection(); public OleDbCommand cmd = new OleDbCommand(); 公共OleDbCommand cmd =新的OleDbCommand();

   public void getConnection()
   {
       con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Project\Database\DataBase.mdb";

   }

private void btnPrint_Click(object sender, EventArgs e)
    {
        DataBase db = new DataBase();
        db.getConnection();


        string Name = txtCostumerName.Text;
        string DayorNight = cboSwimming.Text;
        string Adult1 = txtAdultCount.Text;
        string Kid = txtKidsCount.Text;
        string Cottage1 = cboCottageType.Text;
        string Room = cboRoomType.Text;
        string Total1 = lblCottageTotal.Text;
        string Cash1 = txtCashRecieve.Text;
        string Change1 = txtChange.Text;
        db.con.Open();
       OleDbCommand command = new OleDbCommand ("INSERT INTO  TicketAndCottage (Cotumer_Name , Swimming, Adult, Kids, Cottage, Room , Total, Cash, Change) Values(@Name , @DayorNight , @Adult1 ,@Kid , @Cottage1 , @Room, @Total1 , @Cash1 , @Change1)");
        command.Connection = db.con;
        command.CommandType = CommandType.Text;


        if (db.con.State == ConnectionState.Open)
        {
            command.Parameters.Add("@Cotumer_Name", OleDbType.VarChar, 20).Value = Name;
            command.Parameters.Add("@Swimming", OleDbType.VarChar, 20).Value = DayorNight;
            command.Parameters.Add("@Adult", OleDbType.VarChar, 20).Value = Adult1;
            command.Parameters.Add("@Kids", OleDbType.VarChar, 20).Value = Kid;
            command.Parameters.Add("@Cottage", OleDbType.VarChar, 20).Value = Cottage1;
            command.Parameters.Add("@Room", OleDbType.VarChar, 20).Value = Room;
            command.Parameters.Add("@Total", OleDbType.VarChar, 20).Value = Total1;
            command.Parameters.Add("@Cash", OleDbType.VarChar, 20).Value = Cash1;
            command.Parameters.Add("@Change", OleDbType.VarChar, 20).Value = Change1;

            try
            {
                command.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                db.con.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source);
                //db.con.Close();
            }
        }
        else
        {
            MessageBox.Show("Connection Failed");
        }
    }

and when i click the button i got "Microsoft JET Database Engine" 当我单击按钮时,我得到了“ Microsoft JET数据库引擎”

Without seeing more context (such as what your connection string and data context look like), I can only take a guess. 在没有看到更多上下文(例如连接字符串和数据上下文是什么样的情况)的情况下,我只能猜测一下。

One issue appears to be a typo in the field below called "Cotumer_Name". 一个问题似乎是下面称为“ Cotumer_Name”的字段中的错字。 Your text box is named "CostumerName" with an S in there. 您的文本框名为“ CostumerName”,其中带有S。

OleDbCommand command = new OleDbCommand 
    ("INSERT INTO  TicketAndCottage (Cotumer_Name , Swimming, Adult, Kids, Cottage, Room , Total, Cash, Change) 
                              Values(@Name , @DayorNight , @Adult1 ,@Kid , @Cottage1 , @Room, @Total1 , @Cash1 , @Change1)");

It'd be helpful to include some more code. 包含更多代码会很有帮助。

I figure out whats wrong 我弄清楚怎么了

private void btnPrint_Click(object sender, EventArgs e)
        {
            DataBase db = new DataBase();
            db.getConnection();
            db.con.Open();

            string Name = txtCostumerName.Text;
            string DayorNight = cboSwimming.Text;
            string Adult1 = txtAdultCount.Text;
            string Kid = txtKidsCount.Text;
            string Cottage1 = cboCottageType.Text;
            string Room = cboRoomType.Text;
            double Total1 = Convert.ToDouble(lblCottageTotal.Text);
            double Cash1 = Convert.ToDouble(txtCashRecieve.Text);
            double Change1 = Convert.ToDouble(txtChange.Text);
            OleDbCommand command = new OleDbCommand("INSERT INTO  TicketAndCottage (Costumer_Name , Swimming, Adult, Kids, Cottage, Room , Total, Cash, Change) Values(@Name , @DayorNight , @Adult1 ,@Kid , @Cottage1 , @Room, @Total1 , @Cash1 , @Change1)", db.con);

            if (db.con.State == ConnectionState.Open)
            {
                command.Parameters.Add("@Costumer_Name", OleDbType.VarChar, 50).Value = Name;
                command.Parameters.Add("@Swimming", OleDbType.VarChar, 50).Value = DayorNight;
                command.Parameters.Add("@Adult", OleDbType.VarChar, 50).Value = Adult1;
                command.Parameters.Add("@Kids", OleDbType.VarChar, 50).Value = Kid;
                command.Parameters.Add("@Cottage", OleDbType.VarChar, 50).Value = Cottage1;
                command.Parameters.Add("@Room", OleDbType.VarChar, 50).Value = Room;
                command.Parameters.Add("@Total", OleDbType.Double, 50).Value = Total1;
                command.Parameters.Add("@Cash", OleDbType.Double, 50).Value = Cash1;
                command.Parameters.Add("@Change", OleDbType.Double, 50).Value = Change1;

                try
                {
                    command.ExecuteNonQuery();
                    db.con.Close();
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show(ex.Source);
                    // db.con.Close();
                }
            }
            else
            {
                MessageBox.Show("Connection Failed");
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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