ASP ServerVariables登录用户

时间:2015-07-10 14:25:18

标签: asp.net insert insert-into

在我的代码背后,我有这个

{
    Label2.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; 
}

标识域中的用户名。到现在为止还挺好。它在IIS中正常工作。

但是,我想将用户名存储到数据库中。我怎么能这样做?

这个想法是记录回答这个问题的人:

string insertCmd = "INSERT INTO worker(Business,Business2,Mobile) VALUES (@Business,@Business2,@Mobile)";
using (Conn)
{
    Conn.Open();
    OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
    myCommand.Parameters.AddWithValue("@Business", business.Text);
    myCommand.Parameters.AddWithValue("@Business2", business2.Text);
    myCommand.Parameters.AddWithValue("@Mobile", mobile.Text);
    myCommand.ExecuteNonQuery();
    Label1.Text = "Saved Successfull!";
    Label1.ForeColor = System.Drawing.Color.Green;
}

我将答案插入数据库,但如何保存回答的人?我可以将标签保存到数据库表中吗?或者这是不可能的?

1 个答案:

答案 0 :(得分:0)

只需在表格中添加username字段,然后添加其他参数:

string insertCmd = "INSERT INTO worker(Business,Business2,Mobile,username) VALUES (@Business,@Business2,@Mobile,@username)"; 

using (Conn) { 
  Conn.Open(); 
  OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn); 
  myCommand.Parameters.AddWithValue("@Business", business.Text); 
  myCommand.Parameters.AddWithValue("@Business2", business2.Text); 
  myCommand.Parameters.AddWithValue("@Mobile", mobile.Text); 
  myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name); 
  myCommand.ExecuteNonQuery(); 

  Label1.Text = "Saved Successfull!"; 
  Label1.ForeColor = System.Drawing.Color.Green; 

}