简体   繁体   English

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

[英]I can not add data to access 2007 using C#, can any one help me?

am trying to insert data into Access 2007 using C#, i got some code from stackoverflow and it is really helping to insert data to a single table but i was trying to insert data into two tables which has one to many relationship and the code will run, it has no warring or error but when i click insert button there will be some message box which tells me failed due toSyntax error in insert into statement.can some one fix this thing??? 我正在尝试使用C#将数据插入Access 2007,我从stackoverflow那里得到了一些代码,这确实有助于将数据插入到单个表中,但是我试图将数据插入到具有一对多关系的两个表中,并且代码将运行,它没有任何冲突或错误,但是当我单击“插入”按钮时,将出现一些消息框,告诉我由于insert into语句中的语法错误而失败。可以有人解决此问题吗??? other i have some buttons on form2 such us update,search and when i click one of this button the window will be open n the previous window will not get closed if i click buttons 10 time thr will be ten windows .... pleas am new to this language ni need some help with the codes. 其他我在form2上有一些按钮,例如我们更新,搜索,当我单击该按钮之一时,该窗口将打开n如果单击按钮10次,则前一个窗口将不会关闭。十个窗口将是十个窗口..该语言的新手,您需要代码方面的帮助。

here is my code... 这是我的代码...

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语句的Remark后有一个“,”,我相信这可能会导致无效的语法错误。

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

//this will add a data into multiple table. //这会将数据添加到多个表中。

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

        }

    }
}

} }

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

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