主题:连接数据库失败!
程序浏览时,运行界面可以出来,当点击登录按钮时,出现如下错误:
用户 'COMPUTER\ASPNET' 登录失败。
在网上搜到说问题可能出在web.config设置上,麻烦问下怎样找到web.config文件并设置。
这是一个登录程序,数据库为SQL。
代码如下:
<%@ Page Language="C#" ContentType="text/html" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
private void Page_Load(object sender,EventArgs e)
{
string Rname=Request["n"];
string Rpswd=Request["p"];
if((Rname!=null)&&(Rpswd!=null))
{
username.Text=Rname;
userpswd.Text=Rpswd;
Login_in(sender,e);
}
}
private void Login_in(object sender,EventArgs e)
{
string TempName=username.Text;
string TempPswd=userpswd.Text;
if(TempName==null)
{
Response.Write("<script language='javascript'>alert('请输入用户名和密码');window.history.back();<"+"/script>");
}
else
{
//设定数据库连接
string Connectionstr="server=127.0.0.1;uid=FinanceUser;pwd=FinanceData;database=Finance";
SqlConnection SconnStr=new SqlConnection(Connectionstr);
SqlCommand Scmd=new SqlCommand("select top 1 * fron user() where Name='"+TempName+"'order by ID",SconnStr);
SqlDataReader odr=null;
if(SconnStr.State.ToString()=="Closed")SconnStr.Open();
try
{
//执行命令 获取数据流
odr=Scmd.ExecuteReader();
if(odr.Read())
{
string UserName=odr["Name"].ToString();
string UserPswd=odr["Pswd"].ToString();
string UserLevel=odr["UserLever"].ToString();
if((UserName==TempName)&&(UserPswd==TempPswd))
{
if((UserLevel!="")&&(int.Parse(UserLevel)>0))
{
Session["UserInfo"]=(UserLevel+"0").Substring(0,1)+UserName;
Response.Write("<script language='javascript'>top.location.href='/';<"+"/script>");
}
else
{
Response.Write("<script language='javascript'>alert('你还未通过验证');top.location.href='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('账号密码有误'); window.history.bock();<='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('没有该账号'); window.history.bock();<"+"/script>");
}
odr.Close();
}
catch(Exception ex)
{
if(ex!=null)Response.Write(ex.ToString());
}
SconnStr.Close();
}
Response.End();
}
// Insert page code here
//
void LoginBtb_Click(object sender, EventArgs e) {
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server" text="用户名"></asp:Label>
<asp:TextBox id="username" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label id="Label2" runat="server" text="密 码"></asp:Label>
<asp:TextBox id="userpswd" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="LoginBtb" onclick="Login_in" runat="server" Text="登录"></asp:Button>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
用户 'COMPUTER\ASPNET' 登录失败。
在网上搜到说问题可能出在web.config设置上,麻烦问下怎样找到web.config文件并设置。
这是一个登录程序,数据库为SQL。
代码如下:
<%@ Page Language="C#" ContentType="text/html" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
private void Page_Load(object sender,EventArgs e)
{
string Rname=Request["n"];
string Rpswd=Request["p"];
if((Rname!=null)&&(Rpswd!=null))
{
username.Text=Rname;
userpswd.Text=Rpswd;
Login_in(sender,e);
}
}
private void Login_in(object sender,EventArgs e)
{
string TempName=username.Text;
string TempPswd=userpswd.Text;
if(TempName==null)
{
Response.Write("<script language='javascript'>alert('请输入用户名和密码');window.history.back();<"+"/script>");
}
else
{
//设定数据库连接
string Connectionstr="server=127.0.0.1;uid=FinanceUser;pwd=FinanceData;database=Finance";
SqlConnection SconnStr=new SqlConnection(Connectionstr);
SqlCommand Scmd=new SqlCommand("select top 1 * fron user() where Name='"+TempName+"'order by ID",SconnStr);
SqlDataReader odr=null;
if(SconnStr.State.ToString()=="Closed")SconnStr.Open();
try
{
//执行命令 获取数据流
odr=Scmd.ExecuteReader();
if(odr.Read())
{
string UserName=odr["Name"].ToString();
string UserPswd=odr["Pswd"].ToString();
string UserLevel=odr["UserLever"].ToString();
if((UserName==TempName)&&(UserPswd==TempPswd))
{
if((UserLevel!="")&&(int.Parse(UserLevel)>0))
{
Session["UserInfo"]=(UserLevel+"0").Substring(0,1)+UserName;
Response.Write("<script language='javascript'>top.location.href='/';<"+"/script>");
}
else
{
Response.Write("<script language='javascript'>alert('你还未通过验证');top.location.href='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('账号密码有误'); window.history.bock();<='/';<"+"/script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('没有该账号'); window.history.bock();<"+"/script>");
}
odr.Close();
}
catch(Exception ex)
{
if(ex!=null)Response.Write(ex.ToString());
}
SconnStr.Close();
}
Response.End();
}
// Insert page code here
//
void LoginBtb_Click(object sender, EventArgs e) {
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server" text="用户名"></asp:Label>
<asp:TextBox id="username" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label id="Label2" runat="server" text="密 码"></asp:Label>
<asp:TextBox id="userpswd" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="LoginBtb" onclick="Login_in" runat="server" Text="登录"></asp:Button>
</p>
<!-- Insert content here -->
</form>
</body>
</html>