62,068
社区成员
发帖
与我相关
我的任务
分享
SqlCommand cmd = new SqlCommand("SELECT MAX(userid) FROM table1",cn);
cn.Open();
Label1.Text = cmd.ExecuteScalar().ToString();
cn.Close();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select max(userid) from table1";
DataTable dt = new DataTable();
dt = ReturnDataTable(sql);
this.Label1.Text = dt.Rows[0]["userid"].ToString();
}
/// <summary>
/// 执行有参的查询 返回DataTable
/// </summary>
/// <returns>返回DataTable</returns>
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}