![](/img/trans.png)
[英]My update query is not working. I'm using Visual Studio 2015, and am trying to update an access database
[英]I am trying to update the data but it's not working
我制作了一個程序來保存和更新 Access 數據庫中的數據,我可以保存和讀取數據,我也可以更新,但問題是當我嘗試更新數據時,它會進入第二個條件“數據未更新”。 我已經添加了我的代碼,如果有任何錯誤,請幫助我解決它。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace qasimpos
{
public partial class Form9 : Form
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\pos.accdb;Persist Security Info=True";
string nname, ddate, ccompany, pparty, oorder, kkg, PpCS, ppprice, ccprice, wwprice, rrprice, bbilln, qquantity;
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnup_Click(object sender, EventArgs e)
{
nname = textBox1.Text;
ddate = textBox2.Text;
ccompany = textBox3.Text;
pparty = textBox4.Text;
oorder = textBox5.Text;
kkg = textBox6.Text;
PpCS = textBox7.Text;
ppprice = textBox8.Text;
ccprice = textBox9.Text;
wwprice = textBox10.Text;
rrprice = textBox11.Text;
bbilln = textBox12.Text;
qquantity = textBox13.Text;
//SqlConnection connection = new SqlConnection(connectionString);
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
try
{
string query = "UPDATE prod SET tareekh=@tareekh,company=@company,party=@party,hukam=@hukam,kg=@kg,PCS=@PCS,pprice=@pprice,cprice=@cprice,wprice=@wprice,rprice=@rprice,billn=@billn,quantity=@quantity WHERE namee=@namee";
//SqlCommand cmmd = new SqlCommand(query, connection);
OleDbCommand cmmd = new OleDbCommand(query, connection);
cmmd.Parameters.AddWithValue("@namee", nname);
cmmd.Parameters.AddWithValue("@tareekh", ddate);
cmmd.Parameters.AddWithValue("@company", ccompany);
cmmd.Parameters.AddWithValue("@party", pparty);
cmmd.Parameters.AddWithValue("@hukam", oorder);
cmmd.Parameters.AddWithValue("@kg", kkg);
cmmd.Parameters.AddWithValue("@PCS", PpCS);
cmmd.Parameters.AddWithValue("@pprice", ppprice);
cmmd.Parameters.AddWithValue("@cprice", ccprice);
cmmd.Parameters.AddWithValue("@wprice", wwprice);
cmmd.Parameters.AddWithValue("@rprice", rrprice);
cmmd.Parameters.AddWithValue("@billn", bbilln);
cmmd.Parameters.AddWithValue("@quantity", qquantity);
int result = cmmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Data Updated Successfully ");
this.Close();
}
else
{
MessageBox.Show("Data Not Updated");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
}
private void textBox12_TextChanged(object sender, EventArgs e)
{
}
private void Form9_Load(object sender, EventArgs e)
{
textBox1.Text = nname;
textBox2.Text = ddate;
textBox3.Text = ccompany;
textBox4.Text = pparty;
textBox5.Text = oorder;
textBox6.Text = kkg;
textBox7.Text = PpCS;
textBox8.Text = ppprice;
textBox9.Text = ccprice;
textBox10.Text = wwprice;
textBox11.Text = rrprice;
textBox12.Text = bbilln;
textBox13.Text = qquantity;
}
public Form9(string namee,string tareekh,string company,string party,string hukam,string kg,string PCS,string pprice,string cprice,string wprice,string rprice,string billn,string quantity)
{
InitializeComponent();
nname = namee;
ddate = tareekh;
ccompany = company;
pparty = party;
oorder = hukam;
kkg = kg;
PpCS = PCS;
ppprice = pprice;
ccprice = cprice;
wwprice = wprice;
rrprice = rprice;
bbilln = billn;
qquantity = quantity;
}
}
}
參數應與查詢中提到的相同。
請直接從文檔中找到以下內容
當 CommandType 設置為 Text 時,OLE DB .NET 提供程序不支持將參數傳遞給 SQL 語句或由 OleDbCommand 調用的存儲過程的命名參數。 在這種情況下,必須使用問號 (?) 占位符。 例如:SELECT * FROM Customers WHERE CustomerID = ? 因此,OleDbParameter 對象添加到 OleDbParameterCollection 的順序必須直接對應於命令文本中參數的問號占位符的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.