为何应用程序能访问数据库,而Aspx页面(相同代码)提示登录失败呢?谢谢
Aspx页面代码:
<%@ Page Language= "C# " %>
<%@ Import NameSpace= "System " %>
<%@ Import NameSpace= "System.Data " %>
<%@ Import NameSpace= "System.Data.SqlClient " %>
<Script runat= "server ">
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
DataRow dr;
SqlCommandBuilder cmdbld;
void Page_Load()
{
if(!IsPostBack)
LoadRequestData();
}
void LoadRequestData()
{
lblDT.Text = System.DateTime.Now.Year.ToString()+ "年 "+System.DateTime.Now.Month.ToString()+ "月 "+System.DateTime.Now.Day.ToString()+ "日 ";
lblFN.Text = Request.Form[ "txtFN "];
lblLN.Text = Request.Form[ "txtLN "];
lblSex.Text = "radM ".Equals(Request.Form[ "Sex "])? "Male ": "Female ";
lblInterest.Text = Request.Form[ "ddlInterest "];
}
void btnConfirm_Click(object sender, System.EventArgs e)
{
String strCon = "Data Source = .\\SQLEXPRESS; Initial Catalog = HR; Integrated Security = SSPI; Connect Timeout = 30; ";
con = new SqlConnection(strCon);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From NameList ";
da = new SqlDataAdapter();
ds = new DataSet();
da.SelectCommand = cmd;
con.Open();
da.Fill(ds, "NameList ");
con.Close();
dr = ds.Tables[ "NameList "].NewRow();
dr[ "FirstName "] = lblFN.Text;
dr[ "LastName "] = lblLN.Text;
dr[ "Sex "] = lblSex.Text;
dr[ "Interest "] = lblInterest.Text;
ds.Tables[ "NameList "].Rows.Add(dr);
con.Open();
da.Update(ds, "NameList ");
con.Close();
//cmdbld = new SqlCommandBuilder(da);
//da.UpdateCommand = cmdbld.GetUpdateCommand();
//da.InsertCommand = cmdbld.GetInsertCommand();
//da.DeleteCommand = cmdbld.GetDeleteCommand();
}
</Script>
<html>
<head>
<title> Verification Form </title>
</head>
<body>
<h1 align= "center "> Verification Form </h1>
<form runat= "server ">
<table align= "center " border=1 width=500>
<tr>
<td width=200> Today 's Date </td>
<td> <asp:Label runat= "server " id= "lblDT " /> </td>
</tr>
<tr>
<td> First Name: </td>
<td> <asp:Label runat= "server " id= "lblFN " width= "100% " /> </td>
</tr>
<tr>
<td> Last Name: </td>
<td> <asp:Label runat= "server " id= "lblLN " width= "100% " /> </td>
</tr>
<tr>
<td> Sex: </td>
<td> <asp:Label runat= "server " id= "lblSex " width= "100% " /> </td>
</tr>
<tr>
<td> Interested Area: </td>
<td> <asp:Label runat= "server " id= "lblInterest " width= "100% " /> </td>
</tr>
<tr>
<td> <asp:Button id= "btnConfirm " Text= " Confirm " runat= "server " OnClick= "btnConfirm_Click "/>
<input type= "Button " value= " Back " OnClick= "history.back(-1) "/> </td>
</tr>
</table>
</form>
</body>
</html>
请问为什么连接时提示“无法打开登录所请求的数据库 "HR "。登录失败。用户 'LIUC\ASPNET ' 登录失败。”?
而另一段应用程序代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class DGTrial : Form
{
DataGrid dg;
String strCon;
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
private DGTrial()
{
dg = new DataGrid();
strCon = "Data Source = .\\SQLEXPRESS; Initial Catalog = HR; Integrated Security = SSPI; Connect Timeout = 30; ";
con = new SqlConnection(strCon);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From NameList ";
ds = new DataSet();
da = new SqlDataAdapter();
da.SelectCommand = cmd;
con.Open();
da.Fill(ds, "NameList ");
con.Close();
dg.DataSource = ds.Tables[ "NameList "];
//dg.DataSource = ds.Tables[0];
dg.Dock = DockStyle.Fill;
this.Text = "Datagrid ";
this.Controls.Add(dg);
this.StartPosition =FormStartPosition.CenterScreen;
}
public static void Main()
{
Application.Run(new DGTrial() );
}
}
却能顺利显示。
请众高手指点迷津,多谢!!!
[解决办法]
web上和桌面程序有区别的.
数据库建个帐号,别使用 windows身份验证方式.
[解决办法]
strCon = "server=.;database=HR;uid=leo;pwd=123456;
你先尝试用Sql查询分析器连一下,看你的帐号,密码是否正确
[解决办法]
Persist Security Info=False;Data Source=localhost;Initial Catalog=DBName;User ID=sa;Password = ' '
将其中sa,换成你有权限的用户名.密码不用我说吧..
[解决办法]
看下sql的登陆方式吧:
在企业管理器--操作--属性--安全性选项卡,将身份论证方式改成 "sql server和windows "
[解决办法]
由于没用过2005一直sql2000,帮楼主搜集拉一点答案供参考
用户 'leo ' 登录失败。该用户与可信 SQL Server 连接无关联
如果还不行的话因为:
1 你的2003系统自带的防火墙中没有打开1433端口./
2 你的防毒软件禁用了连接.可以关掉试一下.
或者参观下http://or2.com.cn/blogview.asp?logID=775
[解决办法]
String strCon = "server=.;database=HR;Integrated security=sspi; "