繁体   English   中英

如何检查登录用户是否已回答

[英]How to check if LOGON USER already answered

在我的代码中,我有这个

<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 =“ cmdSave_Click2”时我该怎么做?例如,获取一个警告“用户已回答”的标签

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

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM