我想根据使用C#的ASP.NET Web应用程序中的登录用户来检索日期和时间。我正在使用的代码只是返回第一行详细信息。
我想要当前登录用户的日期和时间,并将其绑定到标签。
我有一个名为Userdatatext
的表格,其中包含3列:
UserName, UserText, LastEditTime
抱歉我的英语不好。
提前致谢:)
我的C#代码
protected void Page_Load(object sender,EventArgs e)
{
if (Session["userName"] != null && Session["userName"] != "")
{
LblUser.Text = "Welcome " + Session["userName"].ToString() + "";
}
else
{
Session.Abandon();
Response.Redirect("Login.aspx");
}
try
{
string Connectionstring = ConfigurationManager.ConnectionStrings["DbLogns"].ToString();
SqlConnection objConection = new SqlConnection(Connectionstring);
objConection.Open();
SqlCommand objCommand = new SqlCommand("select LastEditTime from Userdatatext where UserName='" + Session["userName"] + "'", objConection);
DataSet objDataset = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataset);
string lastdatetime = objDataset.Tables[0].Rows[0][0].ToString();
Lbllastedit.Text = "Last edit on :-" + lastdatetime;
Lbllastedit.Font.Size = 15;
objConection.Close();
}
catch(IndexOutOfRangeException n)
{
Lbllastedit.Text = "Last edit :- no data found !";
Lbllastedit.Font.Size = 13;
}
}
答案 0 :(得分:0)
使用SqlDataReader
:
try
{
string lastdatetime = null;
string Connectionstring = ConfigurationManager.ConnectionStrings["DbLogns"].ToString();
SqlConnection objConection = new SqlConnection(Connectionstring);
objConection.Open();
SqlCommand objCommand = new SqlCommand("select LastEditTime from Userdatatext where UserName='" + Session["userName"] + "'", objConection);
SqlDataReader dr = objCommand.ExecuteReader();
if (dr.Read())
{
lastdatetime = dr["LastEditTime"].ToString();
}
dr.Close();
Lbllastedit.Text = "Last edit on :-" + lastdatetime;
Lbllastedit.Font.Size = 15;
objConection.Close();
}