Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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#_Sql Server 2005 - Fatal编程技术网

C# 第一次登录时的新手。。需要帮助

C# 第一次登录时的新手。。需要帮助,c#,sql-server-2005,C#,Sql Server 2005,当我单击login按钮时,它将执行else中的代码,而不是if 有人能帮我弥补我所缺少的吗 这是我的密码: SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True"); string SQL = "select username,password from login where username

当我单击login按钮时,它将执行
else
中的代码,而不是
if

有人能帮我弥补我所缺少的吗

这是我的密码:

SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True");
        string SQL = "select username,password from login where username like '" + tbUserName.Text + "' and password like '" + tbPassword.Text + "'";
        SqlCommand cmd = new SqlCommand(SQL, cn);
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }
        int roweffect = cmd.ExecuteNonQuery();
        if (roweffect > 0)
        {
            Form2 login_Success = new Form2();
            login_Success.ShowDialog();
        }
        else
        {
            MessageBox.Show("Please provide the correct username and password", "Invalaid Username OR Password", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

    }

ExecuteOnQuery不返回SELECT语句的行数。

用于更新、插入和删除 语句,返回值为 受影响的行数 指挥部。当触发器存在于 正在插入或更新的表 返回值包括 受insert或insert命令影响的行 更新操作和 受触发器影响的行或 触发器。对于所有其他类型的 语句,返回值为-1。如果 发生回滚时,返回值为 还有-1


相反,您可以尝试使用ExecuteScalar并检查结果是否为null。

ExecuteOnQuery不会返回SELECT语句的行数。

用于更新、插入和删除 语句,返回值为 受影响的行数 指挥部。当触发器存在于 正在插入或更新的表 返回值包括 受insert或insert命令影响的行 更新操作和 受触发器影响的行或 触发器。对于所有其他类型的 语句,返回值为-1。如果 发生回滚时,返回值为 还有-1


相反,您可以尝试使用ExecuteScalar并检查结果是否为null。

使用cmd.ExecuteQuery()

使用cmd.ExecuteQuery()

尝试使用
using
语句,而
ExecuteScalar
可能更好

using (SqlConnection cn = new SqlConnection(
            @"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True"))
{
    SqlCommand cmd = new SqlCommand(
        string.Format(@"select username,password 
                        from login where username like '{0}' 
                        and password like '{1}'", 
                            tbUserName.Text, tbPassword.Text), cn);
    cn.Open();
    if (cmd.ExecuteScalar() == null)
        MessageBox.Show("Please provide the correct username and password", 
            "Invalaid Username OR Password", 
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
    else
    {
        Form2 login_Success = new Form2();
        login_Success.ShowDialog();
    }
}

尝试使用
using
语句,而
ExecuteScalar
可能会更好

using (SqlConnection cn = new SqlConnection(
            @"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True"))
{
    SqlCommand cmd = new SqlCommand(
        string.Format(@"select username,password 
                        from login where username like '{0}' 
                        and password like '{1}'", 
                            tbUserName.Text, tbPassword.Text), cn);
    cn.Open();
    if (cmd.ExecuteScalar() == null)
        MessageBox.Show("Please provide the correct username and password", 
            "Invalaid Username OR Password", 
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
    else
    {
        Form2 login_Success = new Form2();
        login_Success.ShowDialog();
    }
}

roweffect变量获取值-1。。我不知道为什么ExecuteOnQuery调用中的select语句总是返回-1,因为它不影响任何行。INSERT、UPDATE和DELETE影响行。select只查看数据。Insert、delete、update语句。请注意,“向我显示数据”与“您更改了多少数据”不同。使用ExecuteQuery/ExecuteScalar,而不是ExecuteOnQuery。roweffect变量获取值-1。。我不知道为什么ExecuteOnQuery调用中的select语句总是返回-1,因为它不影响任何行。INSERT、UPDATE和DELETE影响行。select只查看数据。Insert、delete、update语句。请注意,“向我显示数据”与“您更改了多少数据”不同。使用ExecuteQuery/ExecuteScalar,而不是ExecuteOnQuery。ExecuteScalar()更好,或者ExecuteQuery()?为什么要在sqlconnection之前使用?除非进行更新,否则ExecuteQuery将返回-1(我相信)。ExecuteScalar返回第一行的第一列,这是查看查询是否有结果的良好测试。如果您在Using语句中使用sqlconnection,它将被正确处理,并且连接对象将返回到连接池。executescalar()更好,或者executequery()?为什么在sqlconnection之前使用?除非您进行更新,否则executequery将返回-1(我相信)。ExecuteScalar返回第一行的第一列,这是查看查询是否有结果的良好测试。如果在Using语句中使用SQLConction,则会正确地释放它,并将连接对象返回到连接池。