不能插入或更新值,以动态创建的文本框到SQL Server数据库Windows窗体c#
本文关键字:SQL Server 数据库 窗体 Windows 文本 更新 插入 创建 动态 不能 | 更新日期: 2023-09-27 18:15:17
我已经动态创建了文本框和标签,如果我增加数据库的行数,它们会自动增加。但是现在我想在文本框中插入或更新值,以便在数据库中存储数据。
这是我的代码…
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
}
}
}
}
}
}
这不是一个完美的答案。但你采取的方法有问题。我所理解的是,在这种情况下您不能运行插入命令,因为运行插入命令或向数据库添加新记录意味着您想要向数据库添加新产品,因此您需要首先向表单添加新控件。
则需要识别新添加的控件,然后为该产品运行插入命令。
所以我认为您需要更新数据库中已有的product的值。但是,如果您运行Update命令,又会出现一个问题,即您应该知道在更新产品价格之前必须更新哪个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在这种情况下,你将有更新的记录在网格。您也可以更新它并插入新记录。相信我,这比你想做的要容易得多。