【问题标题】:How To Add Value To A Column And Update Database如何向列添加值并更新数据库
【发布时间】:2016-09-27 23:51:14
【问题描述】:

我正在尝试将 dataGridViewAFp1Annual_Fees 列的值添加到文本框 txtp1AF.Text 中的新值 AF2 中,该值已转换为整数。然后将添加 ResultAF 的结果转换回字符串 updatedAF,这就是要在数据库中更新的值。我确实将变量 AF11 初始化为 0。查询确实更新了选定的列。我们的想法是获取 AF2 中的任何值,并将其添加到我在 数据库 中的 AF11 中特别输入的列和行中的任何值strong> 因此更新值 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 = " "; 
            }

【问题讨论】:

  • 您是否收到错误消息?
  • 我收到Input string not valid,那是在我更新我的代码之前。没有错误消息,值没有加起来
  • 尝试在您的查询中添加括号:[Annual_Fees] 和 [Sponsorship_Status]
  • 查询工作正常,逻辑不是 - 想法是,变量 AF2 中的任何值都将添加到该年费列中的任何值
  • 你的变量 ResultAF, AF11 ,AF2 被声明为 int ?

标签: c# .net sql-server winforms datagridview


【解决方案1】:

假设其他一切都在您身边工作正常,主要问题是您使用包含在您的 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)针对前 2 个数据行从 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 = " ";
            }
        }
    }

【讨论】:

  • 仍然值没有加起来,例如在 Annual_Fees 列中我有 100,然后我输入一个新值 100,即 Annual_Fees中的当前值> 列应该是 200。但它们没有加起来
  • 您是否逐步使用调试器检查中间计算?
  • ` int.TryParse(dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value.ToString(), out AF11);` 结果是 Annual_Fees 列未传递给变量 AF11
  • 但是我怎样才能从 dataTable 的该列中的行中获取值可能会以这种方式工作
  • 如果dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value中有一个点,比如100.00,那么你不能使用int.TryParse(),你必须使用,比如decimal.TryParse()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多