public string Select_Login_Popedom(string Value)

SqlConnection sqlcon =  null;
SqlDataAdapter da = null;
ConDB con=new ConDB();//连接数据库写在里面,绝对没有问题 try
{
sqlcon = new SqlConnection(con.SGTConnection);
sqlcon.Open();
DataSet ds = new DataSet();
da = new SqlDataAdapter("select Popedom from Login where UserName = '"+ Value +"'",sqlcon);
da.Fill(ds);
string Popedom = ds.Tables[0].DefaultView;//关键在这里
sqlcon.Close();
return Popedom;
}
finally
{
sqlcon.Close();
}
}请问有谁能告诉我,这个函数应该怎么写才正确,我想返回查出来的字符

解决方案 »

  1.   

    what are you trying to return? string? or DataView?if you want to return DataView, then usepublic DataView Select_Login_Popedom(string Value)

    SqlConnection sqlcon =  null;
    SqlDataAdapter da = null;
    ConDB con=new ConDB();//连接数据库写在里面,绝对没有问题 try
    {
    sqlcon = new SqlConnection(con.SGTConnection);
    sqlcon.Open();
    DataSet ds = new DataSet();
    da = new SqlDataAdapter("select Popedom from Login where UserName = '"+ Value +"'",sqlcon);
    da.Fill(ds);
    return ds.Tables[0].DefaultView;
    }
    finally
    {
    sqlcon.Close();
    }
    }
    if you try to return some string value, trypublic string Select_Login_Popedom(string Value)

    SqlConnection sqlcon =  null;
    SqlDataAdapter da = null;
    ConDB con=new ConDB();//连接数据库写在里面,绝对没有问题 try
    {
    sqlcon = new SqlConnection(con.SGTConnection);
    sqlcon.Open();
    DataSet ds = new DataSet();
    da = new SqlDataAdapter("select Popedom from Login where UserName = '"+ Value +"'",sqlcon);
    da.Fill(ds);
    string Popedom = String.Empty;
    if (ds.Tables[0].Rows.Count > 0)
    Popedom = ds.Tables[0].Rows["Popedom"].ToString(); return Popedom;
    }
    finally
    {
    sqlcon.Close();
    }
    }or even a simpler solution:public string Select_Login_Popedom(string Value)

    SqlConnection sqlcon =  null;
    SqlDataAdapter da = null;
    ConDB con=new ConDB();//连接数据库写在里面,绝对没有问题 try
    {
    sqlcon = new SqlConnection(con.SGTConnection);
    sqlcon.Open();
    SqlCommand cmd = new SqlCommand("select Popedom from Login where UserName = @UserName", sqlcon);
    cmd.Parameters.Add("@UserName", Value);
    return cmd.ExecuteScalar().ToString();
    }
    finally
    {
    sqlcon.Close();
    }
    }
      

  2.   

    public string Select_Login_Popedom(string Value)

    SqlConnection sqlcon =  null;
    SqlDataAdapter da = null;
    ConDB con=new ConDB();//连接数据库写在里面,绝对没有问题 try
    {
    sqlcon = new SqlConnection(con.SGTConnection);
    sqlcon.Open();
    SqlCommand cmd = new SqlCommand("select Popedom from Login where UserName = @UserName", sqlcon);
    cmd.Parameters.Add("@UserName", Value);
    return cmd.ExecuteScalar().ToString();
    }
    finally
    {
    sqlcon.Close();
    }
    }
    行得通,谢谢你了不过上面那一个语法有错误
      

  3.   

    加上catch返回出错的时候的返回值