如何检查LOGON USER是否已经回答

时间:2015-07-14 12:39:11

标签: c# asp.net ms-access if-statement data-retrieval

在我的代码中,我有这个

<h2> Address Book</h2> <br />
    <table>
    <tr><td><b>User:</b></td><td><asp:Label ID="useren" runat="server"></asp:Label></td></tr> 
    <tr><td><b>Business:</b></td><td><asp:TextBox ID="business" runat="server" MaxLength="13"></asp:TextBox> (Example: 234925xxx)</td></tr>
    <tr><td><b>Business 2:</b></td><td><asp:TextBox ID="business2" runat="server" MaxLength="13"></asp:TextBox> (Example: xxxx)</td></tr>
    <tr><td><b>Mobile:</b></td><td><asp:TextBox ID="mobile" runat="server" MaxLength="13"></asp:TextBox> (Example: 9xxxxxxxx)</td></tr>
    <tr><td colspan="2"><asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label></td></tr>
    <tr><td><asp:Button ID="Button1" runat="server" Text="Save" onclick="cmdSave_Click2" /></td><td></td></tr>
    </table

在我后面的代码中检索登录用户

protected void Page_Load(object sender, EventArgs e)
{
    useren.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
}

这是将文本框和登录用户存储到数据库中

protected void cmdSave_Click2(object sender, EventArgs e)
{
    string sFilePath = Server.MapPath("Database3.accdb");
    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
    string insertCmd = "INSERT INTO Worker(Business,Business2,Mobile,username) VALUES (@Business,@Business2,@Mobile,@useren)";
    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("@useren", HttpContext.Current.User.Identity.Name); 
        myCommand.ExecuteNonQuery(); 
        Label1.Text = "Saved Successfull!";
        Label1.ForeColor = System.Drawing.Color.Green;
    }
}

如何检查登录用户是否已经回答?我的目标是如果登录用户已经存在,那么Access数据库将检索他的数据 或者当我执行按钮onclick =&#34; cmdSave_Click2&#34;如果用户存在反向标签警告&#34;用户已经回答&#34;例如

1 个答案:

答案 0 :(得分:1)

以这种方式尝试。它可能会给你一个解决问题的想法。

protected void cmdSave_Click2(object sender, EventArgs e)
{
  string sFilePath = Server.MapPath("Database3.accdb");
  OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
  con.Open();
  OleDbCommand cmd1 = new OleDbCommand("Select count(*) from Worker where username = '"+HttpContext.Current.User.Identity.Name+"'",con);
  int total = (Int32)cmd1.ExecuteScalar();
  if(total==0)
  {
   OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
  string insertCmd = "INSERT INTO Worker(Business,Business2,Mobile,username) VALUES (@Business,@Business2,@Mobile,@useren)";
  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("@useren", HttpContext.Current.User.Identity.Name); 
    myCommand.ExecuteNonQuery(); 
    Label1.Text = "Saved Successfull!";
    Label1.ForeColor = System.Drawing.Color.Green;
    Conn.Close();
  }
  }
  else
  {
       Label1.Text = "Existing user";
  }
  con.Close();
}
相关问题