Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询中应为“条件”_C#_Asp.net - Fatal编程技术网

C# 查询中应为“条件”

C# 查询中应为“条件”,c#,asp.net,C#,Asp.net,它在dr=cmd.ExecuteReader;附近显示以下错误: 在上下文中指定的非布尔类型的表达式,其中 条件是预期的,在“名称”附近 此处发生的情况将where子句中的用户名替换为[用户名]用户名必须指定为[用户名]或用户名,因此请检查列名一次用户名由两个单词组成。并使用参数化查询 SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\csharp\\L

它在dr=cmd.ExecuteReader;附近显示以下错误:

在上下文中指定的非布尔类型的表达式,其中 条件是预期的,在“名称”附近


此处发生的情况

将where子句中的用户名替换为[用户名]

用户名必须指定为[用户名]或用户名,因此请检查列名一次

用户名由两个单词组成。并使用参数化查询

SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\csharp\\Login1\\App_Data\\Login.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
SqlDataReader dr;   

protected void  LinkButton1_Click(object sender, EventArgs e)
{
 conn.Open();
 cmd=new SqlCommand("Select * from LoginTable where User Name='"+TextBox1.Text+"'",conn);
 dr=cmd.ExecuteReader();   //  <---error here

 if(dr.Read())
 {
   Label1.Text="User name already exist";
   this.Label1.ForeColor=Color.Red;
 }
 else
 {
   Label1.Text="Name available";
 }
}

在此语句中,用户名之间不应留空格:

cmd=new SqlCommand("Select * from LoginTable where [User Name]=@userName",conn);
cmd.Parameters.AddWithValue("@userName", TextBox1.Text);

除了用户名?字段中可能的输入错误外,这不是应该做的。 试试这个:

cmd=new SqlCommand("Select * from LoginTable where **User Name** = '"+TextBox1.Text+"'", conn);

用户名必须指定为[用户名]或用户名,因此请检查列名一次

此外,如果您将保留关键字用作列名,请将其保存在方形黄铜中[]


用户名?应该是用户名。删除列用户名的空格try指定括号,因为它包含空格。从登录表中选择*,其中[User Name]='+TextBox1.Text+',conn;尝试使用[username]而不是用户名更改后,我在同一点上得到了这一点------数据类型text和varchar在equal to运算符中不兼容。
cmd = new SqlCommand("IF EXISTS(SELECT 1 FROM LoginTable where [User Name]=@p)" +
"SELECT 1 ELSE SELECT 0", conn);
cmd.Parameters.AddWithValue("@p", TextBox1.Text);

if(Convert.ToBoolean(cmd.ExecuteScalar()))
{
   Label1.Text="User name already exist";
   this.Label1.ForeColor=Color.Red;
}
else
   Label1.Text="Name available";
cmd=new SqlCommand("Select * from LoginTable where [User Name] = @userName",conn);
cmd.Parameters.AddWithValue("@userName", TextBox1.Text);
cmd=new SqlCommand("Select * from LoginTable where UserName = @userName",conn);
cmd.Parameters.AddWithValue("@userName", TextBox1.Text);