C# 如何为列添加值并更新数据库
我试图将C# 如何为列添加值并更新数据库,c#,.net,sql-server,winforms,datagridview,C#,.net,Sql Server,Winforms,Datagridview,我试图将dataGridViewAFp1中的年费列的值添加到我转换为整数的文本框txtp1AF.Text中的新值AF2。然后,来自加法ResultAF的结果被转换回字符串updatedAF,这是要在数据库中更新的值。我将变量AF11初始化为0。查询不会更新所选列。其思想是获取AF2中的任何值,并将其添加到数据库中AF11中的列和行中的任何值,从而得到更新值updatedAF。以下是我到目前为止所做的,但似乎不起作用。这些值没有相加 private void btnEnterAFp1_Click
dataGridViewAFp1
中的年费列的值添加到我转换为整数的文本框txtp1AF.Text
中的新值AF2。然后,来自加法ResultAF的结果被转换回字符串updatedAF,这是要在数据库中更新的值。我将变量AF11初始化为0。查询不会更新所选列。其思想是获取AF2中的任何值,并将其添加到数据库中AF11中的列和行中的任何值,从而得到更新值updatedAF。以下是我到目前为止所做的,但似乎不起作用。这些值没有相加
private void btnEnterAFp1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtp1AF.Text))
{
MessageBox.Show("Please Enter fee","Oops",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
if (dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value != null)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
int.TryParse(dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value.ToString(), out AF11);
AF2 = Convert.ToInt32(txtp1AF.Text);
ResultAF = AF11 + AF2;
String updatedAF = Convert.ToString(ResultAF);
cmd.CommandText = @"Update P1 set Annual_Fees=@af where Sponsorship_Status = 'Sponsored' OR Sponsorship_Status = 'Unsponsored' OR Sponsorship_Status = 'Formerly Sponsored' ";
cmd.Parameters.AddWithValue("@af", updatedAF);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
//dataGridViewAFp1.DataSource = dt;
conn.Close();
MessageBox.Show("Record Updated Successfully ");
txtp1AF.Text = " ";
}
假设您这边的其他一切都正常工作,主要问题是您正在使用
cmd
变量中包含的Update
命令来更新数据库,以尝试用更新后的值填充dt
。相反,必须使用Select
命令,如下所示:
更改此项:
DataTable dt = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
//dataGridViewAFp1.DataSource = dt;
为此:
DataTable dt = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter("select * from P1", conn);
adap.Fill(dt);
dataGridViewAFp1.DataSource = dt;
编辑:添加完整的工作代码。在此示例中,将前两个数据行的列ListPrice
(列索引9)从100.00
更新为200.00
(监视)。我正在使用您的代码,只是更改表名和列名
private void btnEnterAFp1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AdventureWorks2014.mdf;Integrated Security=True;Connect Timeout=30");
decimal AF2;
decimal AF11;
decimal ResultAF;
if (string.IsNullOrWhiteSpace(txtp1AF.Text))
{
MessageBox.Show("Please Enter fee", "Oops", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (dataGridViewAFp1.Rows[0].Cells[9].Value != null)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
decimal.TryParse(dataGridViewAFp1.Rows[0].Cells[9].Value.ToString(), out AF11);
AF2 = Convert.ToDecimal(txtp1AF.Text);
ResultAF = AF11 + AF2;
String updatedAF = Convert.ToString(ResultAF);
cmd.CommandText = @"Update Production.Product set ListPrice=@af where Name = 'Adjustable Race' OR Name = 'Bearing Ball'";
cmd.Parameters.AddWithValue("@af", updatedAF);
int n = cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter("select * from Production.Product", conn);
adap.Fill(dt);
dataGridViewAFp1.DataSource = dt;
conn.Close();
MessageBox.Show("Record Updated Successfully ");
txtp1AF.Text = " ";
}
}
}
您是否收到错误消息?我收到的
输入字符串无效
,在我将代码更新为该字符串之前。没有错误消息,这些值没有向上添加,以便在您的查询中添加括号:[年费]和[赞助状态]查询工作正常,逻辑不是-想法是,变量AF2中的任何值都会添加到年费列中的任何值。您的变量ResultAF、AF11、AF2声明为int?仍然没有添加值,例如,在年费栏中,我有100,然后我输入一个新值100,年费栏中的当前值应该是200。但是,它们并没有增加。您是否使用调试器检查了中间计算?`int.TryParse(dataGridViewAFp1.Rows[0].Cells[“年费”].Value.ToString(),out AF11);`结果表明,年费列的值没有传递给变量AF11,但如何从dataTable中该列的行中获取值可能是这样的,如果dataGridViewAFp1.rows[0].Cells[“年费”].value
中有一个点,例如100.00
,则不能使用int.TryParse()
,您必须使用,例如,decimal.TryParse()
。