用户名和密码BaseInfo库的Employee里面的数据EmployeeCode和Password,需要实现用户的登录、验证,登陆成功后跳转Module.aspx
解决方案 »
- asp.net MVC中后台控制前台div显示与隐藏
- 向一些企业中的大哥们求一些学习使用项目和常用的代码片断。
- 请问如何修改这个路径
- iis升级到asp.net2.0后,以前用asp.net1.1的程序还能正常运行吗?
- 用VB编写的ASP。NET中判断是否空值用什么函数
- 请教???
- 代码实现发送电子邮件求助!(100分)
- 关于ASP.NET利用SMPT发送带附件的邮件时报错
- ASP.NET MVC4 Controller与View传递强类型的问题,高手请入
- 在Datagrid中加入LinkButton,点击后不触发Datagrid_ItemCommand 事件 !!!!Why????
- asp.net网站外网访问慢
- ASP.NET Page_Load执行两次
//登陆方法
public string[] UserLogin(string uname,string upwd)
{
try
{
SqlParameter[] parmer ={
SqlHelper.CreateCommandParameter("@uname",SqlDbType.VarChar,uname),
SqlHelper.CreateCommandParameter("@upwd",SqlDbType.VarChar,upwd),
};
string str = "Select * from users us,TradeUnion Tu where uname=@uname and upwd=@upwd and us.unitid=Tu.tid";
cmd = SqlHelper.CreateDbCommand(str, conn);
cmd.Parameters.AddRange(parmer);
string[] result = new string[3];
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
result[0] = dr["qx"].ToString();
result[1] = dr["tname"].ToString();
result[2] = dr["unitid"].ToString();
dr.Close();
return result;
}
else
{
return null;
}
//int result = cmd.EndExecuteNonQuery();
//return (result > 0) ? true : false;
}
catch (Exception e)
{ throw e;
}
finally
{
conn.Close();
}
}
//调用
if (UserLogin(Tuser.Text.Trim(),Tpwd.Text.Trim())==null)
{
Response.Write("<script>alert('用户名或密码不正确!');</script>");
Tuser.Text = "";
Tpwd.Text = "";
}
else
{
string[] result = new string[3];
result = u.UserLogin(Tuser.Text.Trim(), Tpwd.Text.Trim());
Session["user"] = Tuser.Text;
Session["qx"] = result[0].ToString();
Session["unit"] = result[1].ToString();
Session["unitid"] = result[2].ToString();
if (result[2].ToString() == "1")
{
Response.Redirect("MyDo.aspx");
}
else
{
Response.Redirect("MyPinfo.aspx");
}
Tuser.Text = "";
Tpwd.Text = "";
}
SqlConnection conn = new SqlConnection(connString);
String sql = "Select Name from "+DB+" where+"條件 ";
SqlCommand comm = new SqlCommand(sql,conn);
try
{
conn.Open();
SqlDataReader sdr = comm.ExecuteReader();
if(sdr.Read())
{
Response。Redirect(網址);
}
conn.Close();
}
catch
{}供參考,不完整
string pwd=this.Pwd.Text;
string sql="select * from Employee where EmployeeCode='"+username+"' and Password='"+pwd"'";
通过是否有返回值来判断是否登录成功。
剩下的自己来完成了
<table align="center">
<tr>
<td>用戶姓名:</td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>用戶ID:</td>
<td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
</tr>
<tr align="center">
<td colspan="2">
<asp:Button ID="btnLogin" runat="server" Text="登入" onclick="btnLogin_Click" />
</td>
</tr>
</table>
后台代码:using System.Data.OracleClient;//连接Oracle数据库,用了Oracle数据库方法
using System.Xml;//使用了XMLprotected void btnLogin_Click(object sender, EventArgs e)
{
string loginName = txtName.Text.Trim();
string Password = txtPassword.Text;
if (loginName != string.Empty)
{
if (Password != string.Empty)
{
OracleConnection conn = new OracleConnection("Password=数据库登录密码;User ID=你自己的数据库登陆名;Data Source=BaseInfo");
OracleCommand comm = new OracleCommand("select * from Employee where EmployeeCode='" + loginName + "' and Password='" + Password + "'", conn);
try
{
conn.Open();
OracleDataAdapter dataAdapter = new OracleDataAdapter();
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = comm;
dataAdapter.Fill(dataSet); if (dataSet.GetXml().ToString().IndexOf("EMPLOYEECODE") >= 0)
{
XmlDocument xmlData = new XmlDocument();
xmlData.LoadXml(dataSet.GetXml().ToString());
string user = xmlData.SelectSingleNode("//EMPLOYEECODE").InnerText;
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JS", "<script>alert('" + user + ",歡迎你的登陸!');window.location='Module.aspx';</script>");
}
else
{
Response.Write("<script>alert('用戶密码不正確!');</script>");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
else
{
Response.Write("<script>alert('請輸入密码!);</script>");
}
}
else
{
Response.Write("<script>alert('請輸入用戶!');</script>");
}
}