在学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是集成验证模式,希望各位能帮我解答,谢谢!!!

解决方案 »

  1.   

    asp.net揭秘的代码
    不解楼主的意思......
      

  2.   

    SQL Server是集成验证模式,也应该有uid 和pwd  啊
      

  3.   

    我把:
       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=;" );
     在我的计算机上试了一下,也不行。 很难猜错在哪里。
            
      

  4.   

    可以啦!!!!把: Integrated Security=SSPI;句掉再运行,就可以啦
      

  5.   

    不过得把你的代码从*.aspx 移到 *.cs代码中去.这是我改正后的代码 ,在*.cs文件中using System;
    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
    }
    }
      

  6.   

    "Server=localhost;Integrated Security=SSPI;database=Pubs;Trusted_Connection=yes""
      

  7.   

    哪位高人说说,"Security=SSPI"怎么解释啊?
      

  8.   

    这段代码确实是asp.net揭密里的,书上用vb.net写的都是要用sa和pwd的,我觉得这样不好,我不喜欢在代码里写上SQL Server等登陆用户和密码,而光盘里的C#的代码就是上面我给的,但是不成功,这种方法我在写c#应用程序的时候是完全可以的,在aps.net中却不行,不解
      

  9.   

    查了好多其他的书,基本上都只介绍了要sa和pwd的方法,上面的程序改成"Server=localhost;uid=sa;pwd=;database=pubs"是可以的,就是找不到另一种方法
      

  10.   

    这种方法在vb.net下这么写完全可以实现,代码如下,但是为什么用C# 这么写还是不行?我要疯掉了
    <%@ 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()
    %>
      

  11.   

    100 MB asp 和 asp .net空间50元/年!,
    同学们可以做一个简单的个人网站用来找工作 
    http: // www.hi876.com 你们也看看吧
      

  12.   

    http://blog.csdn.net/goody9807/archive/2004/12/03/203087.aspx