c# - 链接文本框 C# SQL Server

标签 c# sql-server

如何链接文本框?

场景:

TextBox_Supplier
TextBox_Address

TextBox_Supplier 是自动完成的并且正在工作。在 TextBox_Supplier 中完成输入后,TextBox_Address 将选择供应商的地址。

我的代码不起作用:

private void txb_vendor_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txb_address.Text))
    {
        PurCon.con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = PurCon.getcon();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = String.Format("SELECT address FROM tbl_Supplier WHERE supplier_name = {0}",txb_vendor.Text);

        SqlDataReader red = cmd.ExecuteReader();

        while (red.Read())
        {
            string address = red.GetString(0);
            address = txb_address.Text;
        }

        PurCon.con.Close();
    }
}

谢谢你帮助我!

最佳答案

而不是

address = txb_address.Text;

txb_address.Text = address;

尝试使用参数化查询而不是字符串连接。

关于c# - 链接文本框 C# SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396222/

相关文章:

c# - 如何排队服务器 ASP.NET 完成的工作

c# - 自由格式动态 web.config 区域

asp.net - SQL Server 条件选择语句

sql-server - 删除重复行

c# - SQL 查询输出中的空引用异常

SQL 分组依据和交叉应用

c# - Xamarin Android DownloadString 需要很长时间甚至无法工作

c# - 刷新通知区域中的图标 (Windows CE)

c# - 对 IEnumerable<T> 以外的类型(monads?)进行操作的 LINQ 查询表达式——可能的用途?

sql-server - 如何将 XML 查询结果保存到文件