我无法使用C#向access 2007添加数据,有人能帮我吗?

我无法使用C#向access 2007添加数据,有人能帮我吗?,c#,odbc,C#,Odbc,我尝试使用C#将数据插入Access 2007,我从stackoverflow获得了一些代码,它确实有助于将数据插入到单个表中,但我尝试将数据插入到具有一对多关系的两个表中,代码将运行,它没有任何错误,但当我单击insert按钮时,会出现一些消息框,告诉我由于insert-into语句中的语法错误而失败。有人能解决这个问题吗??? 其他我在form2上有一些按钮,例如更新、搜索,当我单击其中一个按钮时,窗口将打开。如果我单击按钮10次,则上一个窗口将不会关闭。thr将是10个窗口。。。。请告诉我

我尝试使用C#将数据插入Access 2007,我从stackoverflow获得了一些代码,它确实有助于将数据插入到单个表中,但我尝试将数据插入到具有一对多关系的两个表中,代码将运行,它没有任何错误,但当我单击insert按钮时,会出现一些消息框,告诉我由于insert-into语句中的语法错误而失败。有人能解决这个问题吗??? 其他我在form2上有一些按钮,例如更新、搜索,当我单击其中一个按钮时,窗口将打开。如果我单击按钮10次,则上一个窗口将不会关闭。thr将是10个窗口。。。。请告诉我我不熟悉这种语言,我需要一些代码方面的帮助

这是我的密码

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3();
        f3.ShowDialog();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form4 f4 = new Form4();
        f4.ShowDialog();
    }


    private void button4_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Show();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
        @"Data source= C:\Users\user\Desktop\crt_db.accdb";
        try
        {
            conn.Open();
            String Name = txtName.Text.ToString();

            String AR = txtAr.Text.ToString();
            String Wereda = txtWereda.Text.ToString();
            String Kebele = txtKebele.Text.ToString();
            String House_No = txtHouse.Text.ToString();
            String P_O_BOX = txtPobox.Text.ToString();
            String Tel = txtTel.Text.ToString();
            String Fax = txtFax.Text.ToString();
            String Email = txtEmail.Text.ToString();
            String Item = txtItem.Text.ToString();
            String Dep = txtDep.Text.ToString();
            String Remark = txtRemark.Text.ToString();

            String Type = txtType.Text.ToString();
            String Brand = txtBrand.Text.ToString();
            String License_No = txtlicense.Text.ToString();
            String Date_issued = txtDate.Text.ToString();
            String my_querry = "INSERT INTO crtPro(Name,AR,Wereda,Kebele,House_No,P_O_Box,Tel,Fax,Email,Item,Dep,Status,Remark,)VALUES('" + Name + "','" + AR + "','" + Wereda + "','" + Kebele + "','" + House_No + "','" + P_O_BOX + "','" + Tel + "','" + Fax + "','" + Email + "','" + Item + "','" + Dep + "','" + Remark + "')" +
                      "AND INSERT INTO crtItemLicense (" +
                      "Type,Brand,License_No,Date_issued) VALUES('" + Type + "','" + Brand + "','" + License_No + "','" + Date_issued + "') ";

            OleDbCommand cmd = new OleDbCommand(my_querry, conn);
            cmd.ExecuteNonQuery();

            MessageBox.Show("Data saved successfuly...!");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed due to" + ex.Message);
        }
        finally
        {
            conn.Close();

        }

    }
}

}在您的第一个insert语句中,在注释后有一个“,”我相信这可能是导致无效语法错误的原因

String my_querry = "INSERT INTO crtPro(Name,AR,Wereda,Kebele,House_No,P_O_Box,Tel,Fax,Email,Item,Dep,Status,Remark**,**

//这将向多个表中添加数据

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3();
        f3.ShowDialog();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form4 f4 = new Form4();
        f4.ShowDialog();
    }


    private void button4_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Show();
    }
    public void ClearControls()
        {

        txtName.Text=""; // use your id to clear the textbox
        txtItem.Text="";
        txtType.Text = "";
        txtBrand.Text = "";
        txtlicense.Text = "";
        txtDep.Text = "";
        txtDate.Text = "";
        txtRemark.Text = "";
        txtAr.Text = "";
        txtWereda.Text = "";
        txtKebele.Text = "";
        txtHouse.Text = "";
        txtPobox.Text = "";
        txtTel.Text = "";
        txtFax.Text = "";
        txtEmail.Text = "";

        }



    private void button1_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
        @"Data source= C:\Users\user\Desktop\crt_db.accdb";
        try
        {
            conn.Open();
            // for the firs table
            String Name = txtName.Text.ToString();

            String AR = txtAr.Text.ToString();
            String Wereda = txtWereda.Text.ToString();
            String Kebele = txtKebele.Text.ToString();
            String House_No = txtHouse.Text.ToString();
            String P_O_BOX = txtPobox.Text.ToString();
            String Tel = txtTel.Text.ToString();
            String Fax = txtFax.Text.ToString();
            String Email = txtEmail.Text.ToString();
            String Item = txtItem.Text.ToString();
            String Dep = txtDep.Text.ToString();
            String Remark = txtRemark.Text.ToString();
           //for the second table
            String Type = txtType.Text.ToString();
            String Brand = txtBrand.Text.ToString();
            String License_No = txtlicense.Text.ToString();
            String Date_issued = txtDate.Text.ToString();
            String my_querry = "INSERT INTO crtPro(Name,AR,Wereda,Kebele,House_No,P_O_BOX,Tel,Fax,Email,Item,Dep,Remark)VALUES('" + Name + "','" + AR + "','" + Wereda + "','" + Kebele + "','" + House_No + "','" + P_O_BOX + "','" + Tel + "','" + Fax + "','" + Email + "','" + Item + "','" + Dep + "','" + Remark + "')";
            OleDbCommand cmd = new OleDbCommand(my_querry, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            conn.Open();

            String my_querry1 = "SELECT LAST(PID) FROM crtPro";
            OleDbCommand cmd1 = new OleDbCommand(my_querry1, conn);
            string var = cmd1.ExecuteScalar().ToString();
            txtStatus.Text = var;
            String PID = txtStatus.Text.ToString();
            String my_querry2 = "INSERT INTO crtItemLicense(PID,Type,Brand,License_No,Date_issued)VALUES('" +PID + "','" + Type + "','" + Brand + "','" + License_No + "','" + Date_issued + "')";
            OleDbCommand cmd2 = new OleDbCommand(my_querry2, conn);
            cmd2.ExecuteNonQuery();
            MessageBox.Show("Message added succesfully");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed due to" + ex.Message);
        }
        finally
        {
            conn.Close();

        }

    }
}

}

显然不同于SQL,你不能在一条语句中组合插入-有很多方法可以解决()但如果我是你,我会把它们分成两条单独的命令。我试图跟随你,但正如我告诉你的,当我尝试使用“;”时,这是一个错误或者只需将此代码放在一行字符串my_query=“插入crtPro(姓名、AR、Wereda、Kebele、门牌号、邮政信箱、电话、传真、电子邮件、项目、部门、状态、Rema‌​‌​rk)值(“+Name+”、“+AR+”、“+Wereda+”、“+Kebele+”、“+House_No+”、“+P_O_BOX+”、“+Tel+”、“+Fax+”、“+Email+”、“+Item+”、“+Dep+”、“+Remark+”);“在crtItemLicense(类型、品牌、许可证编号、发布日期)值中插入(“+Type+”、“+Brand+”、“+License编号+”、“+Date+发布日期+”)”);好吧,你是对的,我把无用的”,“在说谢谢你之后,你也能帮我在1小时前发布的休息时间吗再次谢谢你NDJ:)