Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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# Can';无法通过给定的用户名查找id_C#_Sql_.net - Fatal编程技术网

C# Can';无法通过给定的用户名查找id

C# Can';无法通过给定的用户名查找id,c#,sql,.net,C#,Sql,.net,我试图通过用户名从名为userDataTbl(在我的MySQL数据库中)的用户表中获取用户id。然而,当我运行下面的代码时,它一直返回-1,结果参数一直是-1,就好像它没有找到用户一样 我还使用了下面的方法来检查表中是否已经存在用户名,然后我只是检查结果是否大于0,以返回它是否存在,或者它是否不存在,但它在那里也不起作用,结果仍然是-1,即使我在表中输入了确实存在的名称。我尝试过调试,但它没有告诉我太多,因为它没有真正向我显示计算机在查询中做了什么,以便我知道它出了什么问题。 代码如下: //r

我试图通过用户名从名为
userDataTbl
(在我的MySQL数据库中)的用户表中获取用户id。然而,当我运行下面的代码时,它一直返回-1,结果参数一直是-1,就好像它没有找到用户一样

我还使用了下面的方法来检查表中是否已经存在用户名,然后我只是检查结果是否大于0,以返回它是否存在,或者它是否不存在,但它在那里也不起作用,结果仍然是-1,即使我在表中输入了确实存在的名称。我尝试过调试,但它没有告诉我太多,因为它没有真正向我显示计算机在查询中做了什么,以便我知道它出了什么问题。 代码如下:

//returns a user id according to a name
public int theId(string name)
{
    SqlCommand command2 = new SqlCommand("Select userId from dbo.userDataTbl where userName=@username", this.connection);
    command2.Parameters.AddWithValue("@username", name);
    int result = command2.ExecuteNonQuery();
    if (result > 0)
    {
        SqlDataReader reader = command2.ExecuteReader();
        reader.Read(); // we have only 1 row
        try
        {
            string foundId = String.Format("{0}", reader["userId"]);
            int id = Convert.ToInt32(foundId);
            return id;
        }
        catch
        {
            return 0;
        }
    }
    else
    {
        return -1;
    }
}
我会非常感激任何帮助,这对我来说真的很重要

基于:

对于UPDATE、INSERT和DELETE语句,返回值是受命令影响的行数。当插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受触发器影响的行数。对于所有其他类型的语句,返回值为-1。如果发生回滚,则返回值也是-1

如果要获取受
命令影响的行数,请选择
命令并将其保存到int变量,您可以使用和:


我认为
int result=command2.ExecuteNonQuery()行导致问题,请删除该行,按如下所示更改代码并运行它

public int theId(string name)
{
    SqlCommand command2 = new SqlCommand("Select userId from dbo.userDataTbl where userName=@username", this.connection);
    command2.Parameters.AddWithValue("@username", name);
    this.connection.open();
    try
    {
        SqlDataReader reader = command2.ExecuteReader();
        if(reader.HasRows)
        {
            while(reader.Read())
            {
                string foundId = String.Format("{0}", reader["userId"]);
                int id = Convert.ToInt32(foundId);
                return id; 
            }
        }
        else
        {
            return -1;
        }
    }
    catch(Exception ex)
    {

    }                
}
希望这能帮助你-快乐编码

public int theId(string name)
{
    SqlCommand command2 = new SqlCommand("Select userId from dbo.userDataTbl where userName=@username", this.connection);
    command2.Parameters.AddWithValue("@username", name);
    this.connection.open();
    try
    {
        SqlDataReader reader = command2.ExecuteReader();
        if(reader.HasRows)
        {
            while(reader.Read())
            {
                string foundId = String.Format("{0}", reader["userId"]);
                int id = Convert.ToInt32(foundId);
                return id; 
            }
        }
        else
        {
            return -1;
        }
    }
    catch(Exception ex)
    {

    }                
}