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 DataBase
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面 Response.Write("<b><center>第一个连接数据库的实例</center></b><br>");
      
SqlConnection myConnection=new SqlConnection("server=localhost;uid=sa;pwd=123;database=northwind");
try
{
myConnection.Open();
Response.Write("连接成功");
}
catch
{
Response.Write("连接失败");
}
} #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
}
}
调试显示连接失败,请高手指点~

解决方案 »

  1.   

    localhost换成127.0.0.1或本机IP或本机名称试试
      

  2.   

    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString)
    在 web.config 配置文件 <configuration> 中加入:
    <connectionStrings>
        <add name="SQLCONNECTIONSTRING" connectionString="data Source=127.0.0.1;database=northwind;user id=sa;pwd=123"
            providerName="System.Data.SqlClient" />
      </connectionStrings>
      

  3.   

    localhost 应该最好是换成 SQL的注册名 本机默认的是 local 远程机器就看装SQL的时候的配置了。
      

  4.   

    先表示感谢~~
    但是
    还是不行呢~~
    连接到数据库SQL SERVER2000时需不需要ODBC数据源驱动呢?
      

  5.   

    localhost换成127.0.0.1
    是可行的~~