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

本文关键字:2007 任何人 都可以 帮助 访问 数据 添加 | 更新日期: 2023-09-27 17:50:23

我试图使用C#将数据插入Access 2007,我从stackerflow获得了一些代码,将数据插入到一个表中确实很有帮助,但我试图将数据插入两个具有一对多关系的表中,代码将运行,它没有交战或错误,但当我点击插入按钮时,会有一些消息框,告诉我由于插入语句中的语法错误而失败。有人能修复这个问题吗???另外,我在form2上有一些按钮,比如更新、搜索,当我点击其中一个按钮时,窗口将打开。如果我点击按钮10次,上一个窗口将不会关闭。。。。Please是这门语言的新手,我需要一些代码方面的帮助。

这是我的密码。。。

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

}

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

在您的第一个插入语句中,Remark后面有一个",",我认为这可能会导致无效语法错误。

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

}