更新我的访问数据库 [英] UPDATE my access database
问题描述
Helloo,
我尝试更新数据库中的所有记录,我使用下面的代码:
Helloo,
I try to update ALL records from my database, i use the folowing code:
try
{
Program.Connection.CommandText = "SELECT * FROM NirProduseAmanet";
DataTable TableIntrareProduse = new DataTable();
Program.Connection.FillDataTable(TableIntrareProduse, true);
string[] ProduseIntare = new string[TableIntrareProduse.Rows.Count];
string[] GrProdusIesire = new string[TableIntrareProduse.Rows.Count];
for (int i = 0; i < TableIntrareProduse.Rows.Count; i++)
{
ProduseIntare[i] = TableIntrareProduse.Rows[i]["PretPropusVanzare"].ToString();
GrProdusIesire[i] = TableIntrareProduse.Rows[i]["Cantitatea"].ToString();
Program.Connection.CommandText = "UPDATE NirProduseAmanet SET PretVanzare=@PretVanzare, GrProdusIesire=@GrProdusIesire";
Program.Connection.AddParameter("@PretVanzare", Convert.ToDecimal(ProduseIntare[i]));
Program.Connection.AddParameter("@GrProdusIesire", Convert.ToDecimal(GrProdusIesire[i]));
Program.Connection.ExecuteNonQuery();
}
}
catch (OleDbException ex)
{
}
代码还可以,我可以从数据库中回收
但是在UPDATE他用数据库中的最后一个值更新所有记录。
任何sugestions?
the code is ok, i retive al recod from database
BUT on UPDATE he update ALL records with my last value from database.
any sugestions?
推荐答案
嗯.. .it will。
当您编写UPDATE时,您必须准确指定哪些记录应该受到影响 - 或者SQL将假设您的意思是所有这些记录,就像您执行SELECT时一样:
Well...it will.
When you write an UPDATE you have to specify exactly which records should be affected - or SQL will assume that you mean all of them, just as it does when you do a SELECT:
SELECT * FROM MyTable
将返回所有记录。
Will return all records.
UPDATE MyTable SET MyColumn=6
将更改所有记录。
如果要限制行你返回,然后使用WHERE子句:
Will change all records.
If you want to limit the rows you return, then you use a WHERE clause:
SELECT * FROM MyTable WHERE MyColumn=6
同样,如果您只想更改特定记录,则使用WHERE子句您的更新:
Similarly, if you want to alter only specific records, you use a WHERE clause on your UPDATE:
UPDATE MyTable SET MyColumn=6 WHERE MyOtherColumn=1
我不知道你的表的哪一列很重要,所以我将由您决定哪一列确定更新标准!
I have no idea what column of your table is significant here, so I leave it up to you to decide which column determines the update criteria!
您忘记添加WHERE
语句来限制/限制变化范围;)
You forgot to addWHERE
statement to limit/restrict the range of changes ;)
UPDATE TableName SET FieldName=NewValue
WHERE FieldName = OldValue
这篇关于更新我的访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!