无法将值动态创建的文本框插入或更新到SQL Server数据库Windows Forms C#中 [英] Can't Insert or update values to dynamically created textbox into SQL Server database Windows Forms C#

查看:90
本文介绍了无法将值动态创建的文本框插入或更新到SQL Server数据库Windows Forms C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created textbox and label dynamically, which can automatically increased if I increase the rows of the database. But now I want to insert or update values into textbox to store data in the database.

这是我的代码...

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);
            }
        }
    }
}

此处是输出。

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

Here is the code for creating textbox dynamically...

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++;
        }
    }


推荐答案

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

This is not a perfect answer. But there are issues with the approach you have taken. What I understand is you cannot run a insert command in this situation, because to run a Insert Command or to add a new record to your database means you wanted to add a new Product to your database for that you need to add new controls to your form first.

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

Than you need to identify the newly added controls and then run a insert command for that product.

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

So I believe you need to update the values of the Products you already have in the Database. But then again there is a problem if you run a Update command is that you should know which ProductID or ProductName has to be updated before updating the Price of the Product.

更新按钮的代码几乎是这样的

The code of the Update Button almost looks like this

点击更新按钮

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。

But the biggest problem is to find the lblvalue corresponding to the textbox. That means if you have updated the value of Banana it should not update apple.

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

So I would recommend you to use DataGridView for this kind of scenario where you will have updated record in the grid. You can update it and Insert new records as well. Believe me its lot easier than what you are trying to do.

这篇关于无法将值动态创建的文本框插入或更新到SQL Server数据库Windows Forms C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆