简体   繁体   English

添加到数据库-具有Access的C#

[英]Adding to a Database - C# with Access

Below is the code I'm using to connect to an Access Database. 下面是我用来连接到Access数据库的代码。 I'm trying to insert the data (which is a form the user fills in). 我正在尝试插入数据(这是用户填写的表格)。 But always the exception is caught and no data gets inserted. 但是总是会捕获异常,并且不会插入任何数据。

I've double checked the variable names for spelling errors,they are all correct. 我仔细检查了变量名是否存在拼写错误,它们都是正确的。

private void addConfirm_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\hospitalApplication\HealthCare.accdb;Persist Security Info=False;";

        String name = typedName.Text;
        String addr = typedAddr.Text;
        String DOB = typedDOB.Text;
        String phone = typedPhone.Text;
        String emergency = typedEmergency.Text;
        String dateOfReg = typedDateReg.Text;
        String patientstatus = typedStatus.Text;
        String bedNo = typedBed.Text;
        String blood = typedBlood.Text;
        String visit = typedVisit.Text;
        String diagnosis = typedDiagnosis.Text;
        String doctorID = typedDoctor.Text;


        OleDbCommand cmd = new OleDbCommand("INSERT into Patients (Full Name, Address,Date of Birth,Phone No,Emergency Contact,Date of Registration,Patient Status,bedID,Blood Type,Date of Visit,Diagnosis,doctorID) Values(@name, @addr,@DOB,@phone,@emergency,@dateOfReg,@patientstatus,@bedNo,@blood,@visit,@diagnosis,@doctorID)", conn);
        cmd.Connection = conn;

        conn.Open();

        if (conn.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("@name", OleDbType.VarChar).Value = name;
            cmd.Parameters.Add("@addr", OleDbType.VarChar).Value = addr;
            cmd.Parameters.Add("@DOB", OleDbType.VarChar).Value = DOB;
            cmd.Parameters.Add("@phone", OleDbType.VarChar).Value = phone;
            cmd.Parameters.Add("@emergency", OleDbType.VarChar).Value = emergency;
            cmd.Parameters.Add("@dateOfReg", OleDbType.VarChar).Value = dateOfReg;
            cmd.Parameters.Add("@patientstatus", OleDbType.VarChar).Value = patientstatus;
            cmd.Parameters.Add("@bedNo", OleDbType.Integer).Value = bedNo;
            cmd.Parameters.Add("@blood", OleDbType.VarChar).Value = blood;
            cmd.Parameters.Add("@visit", OleDbType.VarChar).Value = visit;
            cmd.Parameters.Add("@diagnosis", OleDbType.VarChar).Value = diagnosis;
            cmd.Parameters.Add("@doctorID", OleDbType.Integer).Value = doctorID;

            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                conn.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("Connection Failed");
        }

Your SQL in invalid. 您的SQL无效。 The spaces are confusing the query parser. 这些空格使查询解析器混乱。

The best approach is to not use spaces in your table/column names. 最好的方法是不要在表/列名称中使用空格。 But if you must, then you'll need to enclose them in brackets to tell the query parser that both words identify one item. 但是,如果必须这样做,则需要将它们括在方括号中,以告诉查询解析器两个单词都标识一个项目。 Something like this: 像这样:

INSERT into Patients ([Full Name], ...

Do this for any table/column name which includes either spaces or reserved words. 对包含空格或保留字的任何表/列名称执行此操作。 (You can do this for all of the table/column names if you'd like.) (如果需要,可以对所有表/列名称执行此操作。)

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

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