如何检查是否登录用户已回答 [英] How to check if LOGON USER already answered
本文介绍了如何检查是否登录用户已回答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的code我有这个
In my code I have this
<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
在这背后我的code检索登录用户
In my code behind this to retrieve the logon user
protected void Page_Load(object sender, EventArgs e)
{
useren.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
}
本存储的文本框和登录用户到数据库
This to storage the text box and the logon user into the database
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;
}
}
我如何检查,如果登录用户已经回答?我的目标是,如果登录用户已经存在EM Access数据库检索他的数据
或者我怎样才能使当我执行按钮的onclick =cmdSave_Click2如果用户存在retrive的标签警告了例如已回答用户
How can I check if the logon user already answered? My goal is if the logon user already exist em Access database retrieve his data or how can I make when I execute the button onclick="cmdSave_Click2" if the user exist retrive an label warning up "the user already answered" for example
推荐答案
使这样的尝试。它可能会给你一个想法来解决问题。
Give an attempt in this way. It may give you a idea to solve the issue.
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();
}
这篇关于如何检查是否登录用户已回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文