无法从 Visual C# 将数据插入 MS Access 数据库 [英] Unable to insert data into MS Access database from Visual C#

查看:44
本文介绍了无法从 Visual C# 将数据插入 MS Access 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个编程初学者......我使用一个类来处理连接字符串.

just a beginner in Programing ... I use a class to handle the connectionstring .

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

public class DataBase { public OleDbConnection con = new OleDbConnection(); public OleDbCommand cmd = new 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");
        }
    }

当我点击按钮时,我得到了Microsoft JET 数据库引擎"

and when i click the button i got "Microsoft JET Database Engine"

推荐答案

没有看到更多上下文(比如你的连接字符串和数据上下文是什么样的),我只能猜测.

Without seeing more context (such as what your connection string and data context look like), I can only take a guess.

一个问题似乎是下面名为Cotumer_Name"的字段中的拼写错误.您的文本框名为CostumerName",其中包含一个 S.

One issue appears to be a typo in the field below called "Cotumer_Name". Your text box is named "CostumerName" with an S in there.

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.

这篇关于无法从 Visual C# 将数据插入 MS Access 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆