繁体   English   中英

无法将值动态创建的文本框插入或更新到SQL Server数据库Windows Forms C#中

[英]Can't Insert or update values to dynamically created textbox into SQL Server database Windows Forms C#

我已经动态创建了文本框和标签,如果我增加数据库的行数,它会自动增加。 但是现在我想在文本框中插入或更新值以将数据存储在数据库中。

这是我的代码...

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

using (SqlConnection con = new SqlConnection(cs))
{
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";
        string cmText = "Select Count(ProductId) from tblProduct";

        SqlCommand cmd = new SqlCommand(cmText, con);

        con.Open();
        Int32 count = (Int32)cmd.ExecuteScalar();

        cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        SqlCommand cmd1 = new SqlCommand(cmText, con);

        using (SqlDataReader rdr = cmd1.ExecuteReader())
        {
           //code for dynamically created textbox and label
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        con.Open();

        //TextBox tx = (TextBox) Controls.Find(myTextbox[0], true)[0];
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (this.Controls[i] is TextBox)
            {
                TextBox myTextbox = (TextBox)this.Controls[i];
                string value = myTextbox.Text;

                string cmText = "insert into table tblProduct (UnitPrice) values('" + myTextbox.Text + "')";

                SqlCommand cmd = new SqlCommand(cmText, con);
            }
        }
    }
}

这是输出。

编辑:这是用于动态创建文本框的代码...

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{

    string cmText = "Select Count(ProductId) from tblProductInventory";
    SqlCommand cmd = new SqlCommand(cmText, con);
    con.Open();
    Int32 count = (Int32) cmd.ExecuteScalar();
    int i = 1;
    cmText = "select ProductId,ProductName,UnitPrice from tblProductInventory";
    SqlCommand cmd1 = new SqlCommand(cmText, con);
    using (SqlDataReader rdr = cmd1.ExecuteReader())
    {
        int y = 50;
        Label myLabel = new Label();
        TextBox MyTxt = New TextBox();

        while (rdr.Read())
        {
            myLabel = new Label();
            myLabel.Location = new Point(88, y);
            myLabel.Name = "txtVWReadings" + i.ToString();
            myLabel.Size = new Size(173, 20);
            myLabel.TabIndex = i;
            myLabel.Visible = true;
            myLabel.Text = rdr[1].ToString(); 
            y += 25;
            this.Controls.Add(myLabel);


            MyTxt.Text = rdr[2].ToString(); 
            i++;
        }
    }

如我所见,您正在创建SqlCommand对象,但未得到执行。 您需要添加ExecuteNonQuery()。

private void button1_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        con.Open();
        //TextBox tx = (TextBox) Controls.Find(myTextbox[0], true)[0];
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (this.Controls[i] is TextBox)
            {
                TextBox myTextbox = (TextBox)this.Controls[i];
                string value = myTextbox.Text;
                if (!String.IsNullOrEmpty(value)) 
                {
                string cmText = "insert into tblProduct (UnitPrice) values('" + myTextbox.Text + "')";
                SqlCommand cmd = new SqlCommand(cmText, con);
                int result = cmd.ExecuteNonQuery();
                if(result > 0)
                   {
                       //successful
                   }
                else
                 {
                    //fail
                 }
               } 
            }
        }


    }
}

这不是一个完美的答案。 但是您采用的方法存在问题。 我的理解是,在这种情况下您无法运行插入命令,因为要运行插入命令或向数据库中添加新记录意味着您想要向数据库中添加新产品,因为您需要向其中添加新控件首先形成。

比您需要识别新添加的控件,然后为该产品运行插入命令。

因此,我认为您需要更新数据库中已有产品的值。 但是再一次,如果您运行更新命令,则会出现一个问题,即您应该在更新产品价格之前知道必须更新哪个ProductID或ProductName。

更新按钮的代码几乎如下所示

单击更新按钮

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection connection = new SqlConnection(cs)) using (SqlCommand cmd = connection.CreateCommand())
{ 
    cmd.CommandText = "Update tblProduct SET UnitPrice =@paramunitprice WHERE ProductName = @paramproductname";
    con.Open();
    for (int i = 0; i < this.Controls.Count; i++)
    {
        if (this.Controls[i] is TextBox)
        {
            TextBox myTextbox = (TextBox)this.Controls[i];
            string txtvalue = myTextbox.Text;
            string lblvalue = //The Corresponding Label value to find which UnitPrice has to change.
            cmd.Parameters.AddWithValue("@paramunitprice", txtvalue);
            cmd.Parameters.AddWithValue("@paramproductname", lblvalue);

            cmd.ExecuteNonQuery();
        }
    }
}

但是最大的问题是找到对应于文本框的lblvalue 这意味着,如果您更新了Banana的值,则不应更新apple。

因此,我建议您将DataGridView用于这种情况,在这种情况下您将在网格中有更新的记录。 您可以更新它,也可以插入新记录。 相信我,它比您尝试做的要容易得多。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM