繁体   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