简体   繁体   English

如何在C#中使用datagridview更新Access数据库

[英]How to update Access database using a datagridview in C#

I have a datagridview that bind with a datatable and using DataPropertyName the datagridview only shows 4 columns of database . 我有一个datagridview ,与一个绑定datatable ,并使用DataPropertyNamedatagridview只能显示4列database
this is how I bind database to a datagridview : 这就是我将database绑定到datagridview

string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from SelectedFeeds", Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);

frm.RationFeedsdataGridView.AutoGenerateColumns = false;

frm.RationFeedsdataGridView.Columns[1].Name = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].DataPropertyName = "Feed Name / Description";

frm.RationFeedsdataGridView.Columns[2].Name = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].DataPropertyName = "Quantity";

frm.RationFeedsdataGridView.Columns[3].Name = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].DataPropertyName = "Percent";

frm.RationFeedsdataGridView.DataSource = DTable;

So far nothing wrong, 到目前为止没有错

now I want when user change the value of Columns[2] , it's value automatically inserted (and after that updated) into database I tryed myself using this code but seems that it's wrong, the code is: 现在我想当用户更改Columns[2]的值时,它的值自动插入(并在更新后)到database我尝试使用此代码进行尝试,但似乎不对,代码为:

string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = Connection;
double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
Connection.Open();
Cmd.CommandText="Update SelectedFeeds set Quantity=" +Quantity  'Where Feed Name / Description'= FeedName;
Cmd.ExecuteNonQuery();
Connection.Close();

the first problem is that my CommandText is wrong and I don't know how to fix it. 第一个问题是我的CommandText错误,我不知道如何解决。 what should I do? 我该怎么办?

tanks to @LarsTech the answer is: 坦克@LarsTech的答案是:

public static void UpdateRationFeeds(Form1 frm)
{
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
    OleDbConnection Connection = new OleDbConnection(StrCon);
    OleDbCommand Cmd = new OleDbCommand();
    Cmd.Connection = Connection;
    double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
    string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
    Connection.Open();
    Cmd.CommandText = "Update SelectedFeeds set Quantity=@quantity Where SelectedFeeds.[Feed Name]=@feedname";
    Cmd.Parameters.Add("@quantity", OleDbType.Double).Value = Quantity;
    Cmd.Parameters.Add("@feedname", OleDbType.LongVarChar).Value = FeedName;
    Cmd.ExecuteNonQuery();
    Connection.Close();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#将datagridview更新到数据库? - how to update datagridview to database using c#? 如何在C#中使用datagridview列名更新Access数据库的特定列 - How to update specific columns of access database using datagridview column name in C# c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库 - how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c# 使用 C# 使用 DataGridView 中的值更新数据库 - Update database with values in DataGridView using C# 如何使用C#将DataGridView数据插入Access数据库表中? - How to insert DataGridView data in to table of access database using c#? C#-尝试使用datagridview(也使用OleDbCommandBuilder)更新访问数据库时出现“更新语句中的语法错误” - C# - “Syntax error in update statement” when trying to update the access database using datagridview (also using OleDbCommandBuilder) 使用C#中的datagridview和navigator绑定删除和更新ms Access数据库 - Delete and Update ms access database using a datagridview and navigator binding in c# 使用C#用CSV文件填充DataGridView,并使用结果更新Access数据库 - Using C# to populate a DataGridView with CSV file, and update Access database with results 像Access一样,如何使用ac#datagridview更新数据库文件? - How to use a c# datagridview to update a database file just like Access does? 使用C#将新添加的datagridview行更新到oledb数据库 - Update newly added rows of datagridview to oledb database using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM