use*_*192 -2 c# sql visual-studio
我有一个SELECT COUNT(*)声明,C#/ASP.NET我想将结果存储为int用作IF条件.但是我在visual studio中遇到错误:
错误:System.Data.SqlClient.SqlException(0x80131904):数据类型text和varchar在等于运算符中不兼容.在System.Data.SqlClient.SqlConnection.
它告诉我它发生在int temp行.我在数据库表中访问的列是文本类型.
conn.Open();
String checkEmail = "select count(*) from Players where PlayerEmail= '" + txtEmailLogIn.Text + "'";
SqlCommand com = new SqlCommand(checkEmail, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
conn.Close();
if (temp > 0)
{
}
Run Code Online (Sandbox Code Playgroud)
问题出在您的SQL中.=比较TEXT数据类型时不能使用,而是可以使用LIKE:
String checkEmail = "select count(*) from Players where PlayerEmail LIKE '" + txtEmailLogIn.Text + "'";
Run Code Online (Sandbox Code Playgroud)
但是要注意,在编写像这样的SQL字符串时,你正在打开SQL注入攻击.
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |