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() );
}
}
却能顺利显示。请众高手指点迷津,多谢!!!
<%@ 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() );
}
}
却能顺利显示。请众高手指点迷津,多谢!!!
解决方案 »
- showModalDialog的问题
- Jquery怎么实现倒序?
- 新手问题关于javascript中function方法的网页重定向
- 请教大家,无刷新联动,用的js,后台怎么取第二个downdroplist的值?我取出来的总是空?代码如下!
- 有谁对sqlhelper数据访问层熟悉的高手请进!!
- 请问写的好好的程序为什么会出现如下错误!!急。。。。。。
- Sqlserver进程 占用CPU 100%问题,请大家指点!!
- ASP.NET Portal Starter Kit 管理员的用户名和密码是是吗?
- 如何触发ASP.NET中的TreeView的CheckBox的选中事件?
- 如何为一个 .net 的 Web Application 制作一个安装程序?
- printout打印问题
- 再开一贴动态生成控件
数据库建个帐号,别使用 windows身份验证方式.
还是连接失败,提示“用户 'Leo' 登录失败。该用户与可信 SQL Server 连接无关联。”
请指教,谢谢。
将其中sa,换成你有权限的用户名.密码不用我说吧..
请各位不吝赐教,再次感谢!!!!
在企业管理器--操作--属性--安全性选项卡,将身份论证方式改成"sql server和windows"
用户 'leo' 登录失败。该用户与可信 SQL Server 连接无关联
如果还不行的话因为:
1 你的2003系统自带的防火墙中没有打开1433端口./
2 你的防毒软件禁用了连接.可以关掉试一下.
或者参观下http://or2.com.cn/blogview.asp?logID=775
确定 重启SQL