请教各位,我是ASP.NET新手刚研究SQL 可是第一步就出错!我的连接总是指示错误
无法连接到服务器,难道是我的CONNSTR写错了吗?
server=(local)\\sa;integrated security=SSPI;initial catalog=Northwind
这样子写错了吗?我的SQL服务器的 名称是YUCHENGGANGnorthwind 数据库有一个用户是 SA
系统带的正确的连接应该怎么写?

解决方案 »

  1.   

    server=(local);integrated security=SSPI;initial catalog=Northwind
    SQLSERVER设置混合验证或WINDOWS验证的话,不用写用户名和密码了
    在SQLSERVER中加ASP.NET用户
      

  2.   

    用户 'YUCHENGGANG\ASPNET' 登录失败。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 用户 'YUCHENGGANG\ASPNET' 登录失败。源错误: 
    行 27:  string strsql="select * from Employees";
    行 28:  SqlCommand objcomm=new SqlCommand(strsql,objconn);
    行 29:  objconn.Open();
    行 30:  dgnamelist.DataSource=objcomm.ExecuteReader();
    行 31:  dgnamelist.DataBind();
     源文件: F:\asp\secondfile\WebForm1.aspx    行: 29 堆栈跟踪: 
    [SqlException: 用户 'YUCHENGGANG\ASPNET' 登录失败。]
       System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) +472
       System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) +372
       System.Data.SqlClient.SqlConnection.Open() +384
       ASP.WebForm1_aspx.Page_Load(Object sender, EventArgs e) in F:\asp\secondfile\WebForm1.aspx:29
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Control.OnLoad(EventArgs e) +67
       System.Web.UI.Control.LoadRecursive() +35
       System.Web.UI.Page.ProcessRequestMain() +731 
    这些是错误的 消息.代码如下:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="secondfile.WebForm1" %>
    <%@import namespace="System.Data"%>
    <%@import namespace="System.Data.SqlClient"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <h4>This is my first database exercise</h4>
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:DataGrid id="dgnamelist" style="Z-INDEX: 101; LEFT: 344px; POSITION: absolute; TOP: 144px"
    runat="server" GridLines="None" BackColor="LightBlue" CellPadding="5" CellSpacing="5" BorderWidth="2px"
    BorderColor="Black" ToolTip="include only those employees who are at hq"></asp:DataGrid></FONT>
    </form>
    <script language="C#" runat="server">
    private void Page_Load(object sender,System.EventArgs e)
    {
    string strconnection="data source=10.2.32.249;integrated security=SSPI;User ID=sa;Password=*******;initial catalog=Northwind";
    SqlConnection objconn=new SqlConnection(strconnection);

    string strsql="select * from Employees";
    SqlCommand objcomm=new SqlCommand(strsql,objconn);
    objconn.Open();
    dgnamelist.DataSource=objcomm.ExecuteReader();
    dgnamelist.DataBind();
    objconn.Close();
    }
    </script>
    </body>
    </HTML>
      

  3.   

    <%
    '连接sqlserver
    'StrSQL="DBQ="+server.mappath("data/data.mdb")+";DRIVER={Microsoft Access Driver (*.mdb)};"
    'StrSQL="DSN=news;"'连接Access
    StrSQL="driver={SQL Server};server=(local);uid=;pwd=;database=YUCHENGGANG;"
    set conn=server.createobject("ADODB.CONNECTION")
    conn.open StrSQL
    %>
      

  4.   

    <%
    '连接Access
    'StrSQL="DBQ="+server.mappath("data/data.mdb")+";DRIVER={Microsoft Access Driver (*.mdb)};"
    'StrSQL="DSN=news;"'连接sqlserver
    StrSQL="driver={SQL Server};server=(local);uid=;pwd=;database=YUCHENGGANG;"
    set conn=server.createobject("ADODB.CONNECTION")
    conn.open StrSQL
    %>
      

  5.   

    Dim con As New SqlConnection("server=YUCHENGGANG;uid=sa;pwd=**;database=northwind")
       ' Dim daEmp As New SqlDataAdapter("Select * From Employees", con)
      

  6.   

    server=localhost;uid=sa;pwd=;database=northwind