c# - 在另一个文本框中输入数据时,将 SQL Server 表中的数据添加到文本框中
问题描述
这是我的程序:
我想要做什么:当我输入1
文本框时ProductID
,我想要category
,name
和price
forProductID = 1
被填充到他们的文本框中。
我试图从产品表中读取位置ProductID = ProductIDTB.Text
,然后更改其他文本框以显示其他列中的数据
这是更改 ProductID 文本框时的代码:
protected void ProductIDTB_TextChanged(object sender, EventArgs e)
{
string connectionString1;
SqlConnection cnn1;
connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
cnn1 = new SqlConnection(connectionString1);
string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = ('" + Convert.ToInt32(ProductIDTB.Text) + "') ";
SqlCommand com1 = new SqlCommand(selectSql1, cnn1);
try
{
cnn1.Open();
using (SqlDataReader read = com1.ExecuteReader())
{
while (read.Read())
{
String productcategory = Convert.ToString(read["ProductCategory"]);
ProductCategoryTB.Text = productcategory;
String productname = Convert.ToString(read["ProductName"]);
ProductNameTB.Text = productname;
String productprice = Convert.ToString(read["ProductPrice"]);
ProdPriceTB.Text = productprice;
}
}
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
finally
{
cnn1.Close();
}
}
解决方法:由于 textbox_textchanged 事件不起作用,我决定添加一个使用 ID 查找产品的按钮:
protected void FindProductBtn_Click(object sender, EventArgs e)
{
string connectionString1;
SqlConnection cnn1;
connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
cnn1 = new SqlConnection(connectionString1);
string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = (" + Convert.ToInt32(ProductIDTB.Text) + ") ";
SqlCommand com1 = new SqlCommand(selectSql1, cnn1);
try
{
cnn1.Open();
using (SqlDataReader read = com1.ExecuteReader())
{
while (read.Read())
{
String productcategory = Convert.ToString(read["ProductCategory"]);
ProductCategoryTB.Text = productcategory;
String productname = Convert.ToString(read["ProductName"]);
ProductNameTB.Text = productname;
String productprice = Convert.ToString(read["ProductPrice"]);
ProdPriceTB.Text = productprice;
}
}
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
finally
{
cnn1.Close();
ProductCategoryTB.ReadOnly = true;
ProductNameTB.ReadOnly = true;
ProdPriceTB.ReadOnly = true;
}
}
解决方案
将文本框的 AutoPostBack 属性设置为 true
更多信息:https ://meeraacademy.com/textbox-autopostback-and-textchanged-event-asp-net/
<asp:TextBox ID="ProductIDTB" runat="server" AutoPostBack="True"
OnTextChanged="ProductIDTB_TextChanged"></asp:TextBox>
顺便说一句,SqlParameter
使用参数化查询。除了防止sql注入攻击外,参数化查询还可以帮助RDBMS存储和重用类似查询的执行计划,以确保更好的性能。请参阅:https ://dba.stackexchange.com/questions/123978/can-sp-executesql-be-configured-used-by-default
string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = @productIdFilter";
int productIdFilter = Convert.ToInt32(ProductIDTB.Text);
SqlCommand com1 = new SqlCommand(selectSql1, cnn1);
com1.Parameters.AddWithValue("productIdFilter", productIdFilter);
推荐阅读
- apache-spark - 使用 SPARK 从 S3 分区数据中删除基于分区列的重复项
- amazon-web-services - boto3分页不返回所有条目
- outlook - Outlook for mac - Office.context.mailbox.item.body.prependAsync() 在当前 UI 上工作
- mapbox - 如何找回以前存在的经典风格?
- sql - 如何将一条 SQL 语句设置为其他语句以创建视图
- python - Django 3.1 未正确提供媒体文件
- javascript - 如何使用 JavaScript 使用自定义键在 Firebase 中插入数据?
- bash - 如何使用 sed 或 awk 在模式后删除某些字符?
- javascript - HSL 颜色选择器:背景颜色不变
- python - 两个 TensorFlow 错误