当前位置: 代码迷 >> ASP.NET >> 在建立与服务器的连接时出错,该怎么处理
  详细解决方案

在建立与服务器的连接时出错,该怎么处理

热度:9359   发布时间:2013-02-25 00:00:00.0
在建立与服务器的连接时出错
SqlConnection con = new SqlConnection();
  con.ConnectionString = "server=localhost;database=user ;uid=admin;pwd=''";//本机的用户名为admin 密码为空 
  con.Open();
  string w = ud.Text.ToString();//网页用户名登录的Textbox
  string n = pw.Text.ToString();//网页用户密码
  string str = string.Format("select * from usernp where username='{0}'and userpw='{1}'", w, n);
  SqlCommand conn = new SqlCommand(str, con);
  int k = Convert.ToInt32(conn.ExecuteScalar());
  if (k > 0)
  {
  Session["check"] = 1;
  Response.Redirect("cl.aspx");
  con.Close();
  }
  else
  {
  Session["check"] = 0;
  Response.Redirect("error.aspx");
  con.Close();
  }

“/hzcl”应用程序中的服务器错误。
--------------------------------------------------------------------------------

在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)

源错误: 


行 22: SqlConnection con = new SqlConnection();
行 23: con.ConnectionString = "server=localhost;database=user ;uid=admin;pwd=''";
行 24: con.Open();
行 25: string w = ud.Text.ToString();
行 26: string n = pw.Text.ToString();
 

源文件: f:\hzcl\index.aspx.cs 行: 24 

堆栈跟踪: 


[SqlException (0x80131904): 在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)]
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +173
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199
  System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup) +1069
  System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +606
  System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +193
  System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +502
  System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28
  System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +429
  System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +70
  System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +512
  System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +85
  System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +89
  System.Data.SqlClient.SqlConnection.Open() +160
  _Default.bn_Click(Object sender, EventArgs e) in f:\hzcl\index.aspx.cs:24
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919


--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

------解决方案--------------------------------------------------------
con.ConnectionString = "data source=LOCALHOST\\SQLEXPRESS;User Id=admin;Password='';packet size=4096;database=user";

------解决方案--------------------------------------------------------
con.ConnectionString = "server=.;database=user;uid=admin;pwd=";
这样去试试

还有特别同意4楼的,我们写程序正规的写法都是那样的,在web.config中配置,连接的!
  相关解决方案