gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:45 24 4
gpt4 key购买 nike

我有一个 datagridviewdatatable 绑定(bind)并使用 DataPropertyName datagridview 只显示 4 列数据库.
这就是我将 database 绑定(bind)到 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;

到目前为止没有错,

现在我希望当用户更改 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();

第一个问题是我的 CommandText 错误,我不知道如何修复它。我该怎么办?

最佳答案

感谢@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();
}

关于c# - 如何在 C# 中使用 datagridview 更新 Access 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com