簡體   English   中英

無法從 C# 保存到 MS Access 數據庫

[英]Couldn't save to MS Access database from C#

vith cmb 后面的值是一個組合框。 當我單擊保存按鈕時,它會引發錯誤。

我的代碼在這里:

cn.Open();

OleDbCommand command = new OleDbCommand();
command.Connection = cn;
command.CommandText = "insert into TblProductDetails(ProductID, ProductName, Category, Section, UOM, CostPrice, SellingPrice1, SellingPrice2, DiscountPercentage, DiscountAmount, MinimumPrice, Vendor, Stock) values ('" + txtProductID.Text + "','" + txtName.Text + "','" + category + "','" + section + "','" + uom + "','" + txtCostprice.Text + "','" + txtSellingPrice1.Text + "','" + txtSellingPrice2.Text + "','" + txtDiscountpercentage.Text + "','" + txtDiscountAmount.Text + "','" + txtMinimumPrice.Text + "','" + vendor + "','" + txtBeginingStock.Text + "')";

command.ExecuteNonQuery();
cn.Close();

它可以是很多東西。 請參閱史蒂夫的評論。 但是您還想檢查文本框中“ ' ”字符(撇號)的值,就好像文本框包含該字符一樣,這​​也可能導致語法問題,請查看 SQL 注入以獲取更多信息。 認為這值得一提。 你也可以使用 DataTableAdapter 來處理這種事情,或者實體框架只是為了清除一點(我會這樣做)。

System.Data.OleDb.OleDbConnection conn = new
            System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Your DataBasePath";
        conn.Open();
        System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "INSERT INTO TblProductDetails (ProductID, ProductName, Category, Section, UOM, CostPrice, SellingPrice1, SellingPrice2, DiscountPercentage, DiscountAmount, MinimumPrice, Vendor, Stock) VALUES(@ProductID, @ProductName, @Category, @Section, @UOM, @CostPrice, @SellingPrice1, @SellingPrice2, @DiscountPercentage, @DiscountAmount, @MinimumPrice, @Vendor, @Stock)";
        cmd.Parameters.AddWithValue("@ProductID", comboBox1.Text);
        cmd.Parameters.AddWithValue("@ProductName", textBox1.Text);
        cmd.Parameters.AddWithValue("@Category", textBox2.Text);
        cmd.Parameters.AddWithValue("@Section", textBox2.Text);
        cmd.Parameters.AddWithValue("@UOM", textBox4.Text);
        // continue Your Code its just example 
        cmd.Connection = conn;

        cmd.ExecuteNonQuery();
        conn.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM