3 回答
TA贡献1801条经验 获得超16个赞
如果你的用户名在数据库里面是唯一的就可以使用此方法,如果不是就不行,此方法放回:查询所返回的结果集中第一行的第一列。 忽略其他列或行。如果用户名唯一就可以如下:
string sql ="SELECT Uid FROM User where Uname=@Uname"
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Uname", SqlDbType.VarChar);
cmd.Parameters["@Uname"].Value = 要查的用户名;
try
{
conn.Open();
int Uid = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
TA贡献1757条经验 获得超8个赞
string s = "server=.;database=SampleDb;integrated security=true;";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = s;
SqlCommand cmd = new SqlCommand();
string sql = "select count(*) from student";
cmd.CommandText = sql;
cmd.Connection = conn;
conn.Open();
object o = cmd.ExecuteScalar();
int n = (int)o;
conn.Close();
- 3 回答
- 0 关注
- 367 浏览
添加回答
举报