在学asp.net的时候在书上看到访问SQL Server的一种方法,代码如下
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data.SqlClient" %><script runat=server>
void Page_Load(Object sender , EventArgs e)
{
SqlConnection conPubs;
SqlCommand cmdSelectAuthors;
SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Pubs" );
conPubs.Open();
cmdSelectAuthors = new SqlCommand( "Select au_lname From Authors", conPubs );
dtrAuthors = cmdSelectAuthors.ExecuteReader();
while ( dtrAuthors.Read())
{
Response.Write( "<li>" );
Response.Write( dtrAuthors[ "au_lname" ] );
}
dtrAuthors.Close();
conPubs.Close();
}
</script>
但是失败,而我用这种方法写c#的应用程序的时候是可以实现的,我的SQL Server是集成验证模式,希望各位能帮我解答,谢谢!!!
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data.SqlClient" %><script runat=server>
void Page_Load(Object sender , EventArgs e)
{
SqlConnection conPubs;
SqlCommand cmdSelectAuthors;
SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Pubs" );
conPubs.Open();
cmdSelectAuthors = new SqlCommand( "Select au_lname From Authors", conPubs );
dtrAuthors = cmdSelectAuthors.ExecuteReader();
while ( dtrAuthors.Read())
{
Response.Write( "<li>" );
Response.Write( dtrAuthors[ "au_lname" ] );
}
dtrAuthors.Close();
conPubs.Close();
}
</script>
但是失败,而我用这种方法写c#的应用程序的时候是可以实现的,我的SQL Server是集成验证模式,希望各位能帮我解答,谢谢!!!
不解楼主的意思......
1.<script runat=server> 改为:<script runat=server language=c#>
2.conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Pubs" );
改为:
conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Pubs;uid=sa; pwd=;" );
在我的计算机上试了一下,也不行。 很难猜错在哪里。
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace WebApplication20
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection conPubs;
SqlCommand cmdSelectAuthors;
SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;database=Pubs;uid=sa;pwd=;" );
conPubs.Open();
cmdSelectAuthors = new SqlCommand( "Select au_lname From Authors", conPubs );
dtrAuthors = cmdSelectAuthors.ExecuteReader();
while ( dtrAuthors.Read())
{
Response.Write( "<li>" );
Response.Write( dtrAuthors[ "au_lname" ] );
}
dtrAuthors.Close();
conPubs.Close(); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
<%@ Import Namespace="System.Data.SqlClient" %>
<%
Dim conPubs As SqlConnection
Dim cmdSelectAuthors As SqlCommand
Dim dtrAuthors As SqlDataReaderconPubs = New SqlConnection("server=(local);database=pubs;Trusted_Connection=yes" )
conPubs.Open()
cmdSelectAuthors = New SqlCommand( "Select au_lname From Authors", conPubs )
dtrAuthors = cmdSelectAuthors.ExecuteReader()
While dtrAuthors.Read()
Response.Write( "<li>" )
Response.Write( dtrAuthors( "au_lname" ) )
End While
dtrAuthors.Close()
conPubs.Close()
%>
同学们可以做一个简单的个人网站用来找工作
http: // www.hi876.com 你们也看看吧