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"/>
&nbsp;&nbsp;&nbsp;<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() );
}
}
却能顺利显示。请众高手指点迷津,多谢!!!

解决方案 »

  1.   

    web上和桌面程序有区别的.
     数据库建个帐号,别使用 windows身份验证方式.
      

  2.   

    谢谢楼上高手,我将连接字符串改为Data Source = .\\SQLEXPRESS; Initial Catalog = HR; uid=Leo; pwd=123456; Connect Timeout = 30;
    还是连接失败,提示“用户 'Leo' 登录失败。该用户与可信 SQL Server 连接无关联。”
    请指教,谢谢。
      

  3.   

    strCon = "server=.;database=HR;uid=leo;pwd=123456;你先尝试用Sql查询分析器连一下,看你的帐号,密码是否正确
      

  4.   

    Persist Security Info=False;Data Source=localhost;Initial Catalog=DBName;User ID=sa;Password =''
    将其中sa,换成你有权限的用户名.密码不用我说吧..
      

  5.   

    我用的是SqlServer2005Express,使用Sql Server Management Studio Express,在安全性->登录名中增加了Leo这个用户,并将密码设为123456.可是提示“用户Leo登录失败,该用户与可信Sql Server连接无关联。Error18452”。
    请各位不吝赐教,再次感谢!!!!
      

  6.   

    看下sql的登陆方式吧:
    在企业管理器--操作--属性--安全性选项卡,将身份论证方式改成"sql server和windows"
      

  7.   

    由于没用过2005一直sql2000,帮楼主搜集拉一点答案供参考
     用户 'leo' 登录失败。该用户与可信 SQL Server 连接无关联    
    如果还不行的话因为:  
    1 你的2003系统自带的防火墙中没有打开1433端口./ 
    2 你的防毒软件禁用了连接.可以关掉试一下. 
    或者参观下http://or2.com.cn/blogview.asp?logID=775
      

  8.   

    String strCon = "server=.;database=HR;Integrated security=sspi;"
      

  9.   

    我现在直接用Sql Server Management Studio Express登录,用户名Leo,密码123456,可还是登录不了。有哪位高人告诉我具体如何设置啊?拜谢!!
      

  10.   

    下决心装个SqlServer2005吧,Express版本可能稍稍逊了一点儿……再次谢谢各位的悉心指导
      

  11.   

    在数据库上面 属性-----显示服务器属性-----安全性里 选择 sql和windows 
    确定  重启SQL