我无法使用 C# 添加数据以访问 2007,任何人都可以帮助我吗?

Posted

技术标签:

【中文标题】我无法使用 C# 添加数据以访问 2007,任何人都可以帮助我吗?【英文标题】:I can not add data to access 2007 using C#, can any one help me? 【发布时间】:2014-03-14 08:02:48 【问题描述】:

我正在尝试使用 C# 将数据插入 Access 2007,我从 *** 获得了一些代码,它确实有助于将数据插入到单个表中,但我试图将数据插入到两个具有一对多关系的表中,并且代码将运行,它没有争吵或错误,但是当我单击插入按钮时,会有一些消息框告诉我由于插入语句中的语法错误而失败。有人可以解决这个问题吗? 其他我在form2上有一些按钮,例如我们更新,搜索,当我单击此按钮中的一个时,窗口将打开n如果我单击按钮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();

        

    

【问题讨论】:

meta.stackexchange.com/questions/10647/… 显然与 SQL 不同,你不能在一个语句中组合插入 - 有办法绕过它 (***.com/questions/62504/…) 但如果我是你,我会让它们成为 2 个单独的命令 确定 NDJ,我正在尝试关注你,但正如我告诉你的那样,当我尝试使用 ';' 时出现错误或者只是把这段代码放在一行 String my_query = "INSERT INTO crtPro(Name,AR,Wereda,Kebele,House_No,P_O_Box,Tel,Fax,Email,Item,Dep,Status,Rema‌​‌​rk)VALUES('" + 姓名 + "','" + AR + "','" + Wereda + "','" + Kebele + "','" + House_No + "','" + P_O_BOX + "','" + 电话+ "','" + 传真 + "','" + 电子邮件 + "','" + 项目 + "','" + Dep + "','" + 备注 + "')"; "INSERT INTO crtItemLicense (Type,Brand,License_No,Date_issued) VALUES('" + Type + "','" + Brand + "','" + License_No + "','" + Date_issued + "')"; 【参考方案1】:

在您的第一个插入语句中,Remark 后有一个“,”,我相信这可能会导致无效的语法错误。

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

【讨论】:

嗯,你是对的,我把无用的“,”在评论之后谢谢你,你能帮我解决一下 1 小时前发布的休息时间,再次感谢 NDJ :)【参考方案2】:

//这会将数据添加到多个表中。

    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();

        

    

【讨论】:

以上是关于我无法使用 C# 添加数据以访问 2007,任何人都可以帮助我吗?的主要内容,如果未能解决你的问题,请参考以下文章

Sharepoint 2007 alertme (通知我) 无法正常工作

用Javascript填充数据以折叠

无法使用 MS Access 2007 访问 ODBC DB

RoR:发送附加数据以使用 Jquery AJAX 提交

django test - 如何获取响应数据以供将来使用

具有 Ms 访问权限的 C# Windows 窗体应用程序无法插入数据