ASP.NET c#连接ACCESS数据库的登陆按钮代码
protectedvoidButton1_Click(objectsender,EventArgse){stringUserName=this.username.Text...
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = this.username.Text;
string PassWord = this.password.Text;
string connstr = "provider=microsoft.jet.oledb.4.0;data source="+ Server.MapPath("zhongtian_site.mdb");
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
string sqlstr = "select 用户名 from 用户表 where 用户名='" + UserName + "'and 密码='" + PassWord + "'"
OleDbCommand cmd = new OleDbCommand(sqlstr,conn);
if (cmd.ExecuteScalar()>0)
{
Response.Redirect("形态中心2.aspx");
}
else
{
Response.Write("密码或帐号错误");
}
}
请指正 展开
{
string UserName = this.username.Text;
string PassWord = this.password.Text;
string connstr = "provider=microsoft.jet.oledb.4.0;data source="+ Server.MapPath("zhongtian_site.mdb");
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
string sqlstr = "select 用户名 from 用户表 where 用户名='" + UserName + "'and 密码='" + PassWord + "'"
OleDbCommand cmd = new OleDbCommand(sqlstr,conn);
if (cmd.ExecuteScalar()>0)
{
Response.Redirect("形态中心2.aspx");
}
else
{
Response.Write("密码或帐号错误");
}
}
请指正 展开
3个回答
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = this.username.Text;
string PassWord = this.password.Text;
string connstr = "provider=microsoft.jet.oledb.4.0;data source="+ Server.MapPath("zhongtian_site.mdb");
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
int count = 0;
string sql = string.Format("select count(*) from table where userName ='{0}' and userPwd={1}",UserName ,PassWord );
SqlCommand command = new SqlCommand(sql,connstr);
try
{
conn.Open();
count = (int)command.ExecuteScalar();
if (count == 1)
{
Response.Redirect("形态中心2.aspx");
}
else
{
Response.Write("密码或帐号错误");
}
}
catch(Exception ex)
{
Response.Write("密码或帐号错误");
}
finally
{
conn.Close();
}
}
{
string UserName = this.username.Text;
string PassWord = this.password.Text;
string connstr = "provider=microsoft.jet.oledb.4.0;data source="+ Server.MapPath("zhongtian_site.mdb");
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
int count = 0;
string sql = string.Format("select count(*) from table where userName ='{0}' and userPwd={1}",UserName ,PassWord );
SqlCommand command = new SqlCommand(sql,connstr);
try
{
conn.Open();
count = (int)command.ExecuteScalar();
if (count == 1)
{
Response.Redirect("形态中心2.aspx");
}
else
{
Response.Write("密码或帐号错误");
}
}
catch(Exception ex)
{
Response.Write("密码或帐号错误");
}
finally
{
conn.Close();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<link href="css.css" rel="stylesheet" type="text/css">
</HEAD>
<body MS_POSITIONING="GridLayout">
<FONT face="宋体"><center>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
string txtuser = this.txtuser.Text.ToString();
string txtpsw = this.txtpsw.Text.ToString();
string connsql="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
connsql+=Server.MapPath(ConfigurationSettings.AppSettings["database"]);
string cmdstring = "select * from gly where zh = '"+txtuser+"' and mm='"+txtpsw+"'";
OleDbConnection connection = new OleDbConnection(connsql);
OleDbCommand sqlcmd = new OleDbCommand(cmdstring, connection);
OleDbDataReader rd;
connection.Open();
rd = sqlcmd.ExecuteReader();
if (rd.Read())
{
Session["username"] = txtuser;
Response.Redirect("Default.aspx");
}
else
{
this.Msg.Text = "Wrong username or password!";
}
}
</script>
<form runat="server" ID="Form1">
<table><tr><td width="500">
登录名
<asp:TextBox id="txtuser" runat="server" ></asp:TextBox></td></tr><tr><td>
密 码<asp:TextBox id="txtpsw" runat="server" TextMode="Password"></asp:TextBox></td></tr><tr><td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" /></asp:Button>
<font face="宋体">
<asp:Label ID="Msg" runat="server" ></asp:Label>
</font></td></tr></table>
</form></center>
</FONT>
</body>
</HTML>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<link href="css.css" rel="stylesheet" type="text/css">
</HEAD>
<body MS_POSITIONING="GridLayout">
<FONT face="宋体"><center>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
string txtuser = this.txtuser.Text.ToString();
string txtpsw = this.txtpsw.Text.ToString();
string connsql="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
connsql+=Server.MapPath(ConfigurationSettings.AppSettings["database"]);
string cmdstring = "select * from gly where zh = '"+txtuser+"' and mm='"+txtpsw+"'";
OleDbConnection connection = new OleDbConnection(connsql);
OleDbCommand sqlcmd = new OleDbCommand(cmdstring, connection);
OleDbDataReader rd;
connection.Open();
rd = sqlcmd.ExecuteReader();
if (rd.Read())
{
Session["username"] = txtuser;
Response.Redirect("Default.aspx");
}
else
{
this.Msg.Text = "Wrong username or password!";
}
}
</script>
<form runat="server" ID="Form1">
<table><tr><td width="500">
登录名
<asp:TextBox id="txtuser" runat="server" ></asp:TextBox></td></tr><tr><td>
密 码<asp:TextBox id="txtpsw" runat="server" TextMode="Password"></asp:TextBox></td></tr><tr><td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" /></asp:Button>
<font face="宋体">
<asp:Label ID="Msg" runat="server" ></asp:Label>
</font></td></tr></table>
</form></center>
</FONT>
</body>
</HTML>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加断点试试 看看具体哪里的问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询