gpt4 book ai didi

c# - 数据类型 text 和 varchar 在 C# 中的等于运算符中不兼容

转载 作者:可可西里 更新时间:2023-11-01 03:14:06 24 4
gpt4 key购买 nike

我正在尝试从 employeeTable 访问数据 empname,但我编写的代码出现以下错误:

The data types text and varchar are incompatible in the equal to operator.

请提出解决方案

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string Connection = "Data Source=(local);Initial catalog=Test;Integrated Security=true";
string Query = "SELECT * FROM EmployeeTable WHERE empname='" + comboBox1.Text + "' ;";

SqlConnection conn = new SqlConnection(Connection);
SqlCommand cmd = new SqlCommand(Query, conn);

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
textBoxEmpName.Text = reader["EmpName"].ToString();
}
}

最佳答案

您不能将 text 与 varchar 进行比较,但作为对将来遇到此问题的任何人的回答,只需将 text 列转换为 varchar 以进行查询。

SELECT * FROM  EmployeeTable WHERE CONVERT(VARCHAR, empname) = '" + comboBox1.Text + "' ;";

始终使用 parameters

SELECT * FROM  EmployeeTable WHERE CONVERT(VARCHAR, empname) = @comboBox";

关于c# - 数据类型 text 和 varchar 在 C# 中的等于运算符中不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com