将数据插入相关数据库。 C#

时间:2017-12-07 12:23:09

标签: c# sql ms-access

我有两张桌子,我试图用我的C#应用​​程序插入数据。他们与身份证相关。

我已经阅读了关于如何执行此操作的一些问题,但没有一个有效,我现在决定将它们作为单独的SQL查询来执行。

 private void btnSave_Click(object sender, EventArgs e)
        {
            //table 1
            String acctCode = tbAcctCode.Text;
            String address1 = tbAdd1.Text;
            String address2 = tbAdd2.Text;
            String address3 = tbAdd3.Text;
            String balance = tbBalance.Text;
            String yearSales = tbSales.Text;
            String yearCost = tbCost.Text;
            //table 2
            String date = tbDate.Text;
            String transaction = tbTrans.Text;
            String docNum = tbDocNum.Text;
            String grossVal = tbGross.Text;
            String vatVal = tbVat.Text;

            //SQL statement set to Add a new value
            sql = String.Format("INSERT INTO "+ table1 + "(Account_Code,Address1,Address2,Address3,Balance,Sales_Year_To_Date,Cost_Year_To_Date) " +
                                 "VALUES " +
                                 "('{0}'," + "{1}," + "{2}," + "'{3}'," + "'{4}'," + "{5}," + "'{6}');", acctCode, address1, address2, address3, balance, yearSales, yearCost);

            sql2 = String.Format("INSERT INTO " + table2 + "(Date,Transaction_Type,Document_No,Gross_Transaction_Value,Vat_Value) " +
                                 "VALUES " +
                                 "('{0}'," + "'{1}'," + "'{2}'," + "'{3}'," + "'{4}');", date, transaction, docNum, grossVal, vatVal);

            try
            {
                dbConn = new OleDbConnection(conString);
                dbCmd = new OleDbCommand(sql, dbConn);

                dbConn.Open();
                dbCmd.ExecuteNonQuery();
                dbCmd = new OleDbCommand(sql2, dbConn);
                dbCmd.ExecuteNonQuery();
            }
            catch (SqlException E)
            {
                MessageBox.Show(E.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                dbConn.Close();
            }

        }
    }

数据很好地插入到第一个表中,但是问题出现在sql2的OleDbCommand中。 "它在INSERT INTO语句中抛出System.Data.OleDb.OleDbException:'语法错误。'"

我感觉我的sql语法错误但我无法发现它。

1 个答案:

答案 0 :(得分:1)

Try This Would Be Prefect

private void btnSave_Click(object sender, EventArgs e)
{
    //SQL statement set to Add a new value
        sql = String.Format("INSERT INTO " + table1 + "(Account_Code,Address1,Address2,Address3,Balance,Sales_Year_To_Date,Cost_Year_To_Date) " +
                             "VALUES " + (@acctCode, @address1, @address2, @address3, @balance, @yearSales, @yearCost));

        sql2 = String.Format("INSERT INTO " + table2 + "([Date],Transaction_Type,Document_No,Gross_Transaction_Value,Vat_Value)" +
                             "VALUES " + (@date, @transaction, @docNum, @grossVal, @vatVal));


        try
        {
            dbConn = new OleDbConnection(conString);
            cmd = new OleDbCommand(sql, dbConn);

            //table 1
            cmd.Parameters.AddWithValue("@acctCode", tbAcctCode.Text);
            cmd.Parameters.AddWithValue("@address1", tbAdd1.Text);
            cmd.Parameters.AddWithValue("@address2", tbAdd2.Text);
            cmd.Parameters.AddWithValue("@address3", tbAdd3.Text);
            cmd.Parameters.AddWithValue("@balance", tbBalance.Text);
            cmd.Parameters.AddWithValue("@yearSales", tbSales.Text);
            cmd.Parameters.AddWithValue("@yearCost", tbCost.Text);

            dbConn.Open();
            cmd.ExecuteNonQuery();
            cmd = new OleDbCommand(sql2, dbConn);

            //table 2
            cmd.Parameters.AddWithValue("@date", tbDate.Text);
            cmd.Parameters.AddWithValue("@transaction", tbTrans.Text);
            cmd.Parameters.AddWithValue("@docNum", tbDocNum.Text);
            cmd.Parameters.AddWithValue("@grossVal", tbGross.Text);
            cmd.Parameters.AddWithValue("@vatVal", tbVat.Text);

            cmd.ExecuteNonQuery();
        }
        catch (SqlException E)
        {
            MessageBox.Show(E.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            dbConn.Close();
        }
    }