provider:SQL网络接口,错误26 - 找到指定的服务器/实例时出错

时间:2012-09-25 15:16:42

标签: sql-server

try
        {
            //Create our connection strings
            string sSqlConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)) + "\\ClaimFiles.mdf;Integrated Security=True;User Instance=True";
            MessageBox.Show(sSqlConnectionString);

            //Execute a query to erase any previous data from our destination table
            string sClearSQL = "DELETE FROM PA";
            SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
            SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
            SqlConn.Open();
            MessageBox.Show(SqlCmd.ExecuteNonQuery().ToString());
            SqlConn.Close();
        }
        catch (SqlException ex)
        {
            //handle exception
            StringBuilder errorMessages = new StringBuilder();

            for (int i = 0; i < ex.Errors.Count; i++)
            {
                errorMessages.Append("Index #: " + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "ErrorNumber: " + ex.Errors[i].Number + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Severity Level: " + ex.Errors[i].Class + "\n" +
                    "Server:" + ex.Errors[i].Server + "\n");
                MessageBox.Show(errorMessages.ToString());
            }
        }

上面是我在C#中的代码,我正在使用Microsoft SQL express。点击即可激活上面的代码。当我在Visual Studio中运行代码时,一切正常。但是当我将项目的文件夹复制到另一台计算机(操作系统:Windows XP)并运行.exe文件时,程序会捕获一个SqlException:

  

建立与服务器的连接时发生错误。连接到SQL Server 2005时,此错误可能是由于在默认设置下SQL Server不允许远程连接。 (提供程序:SQL网络接口,错误26 - 错误定位指定的服务器/实例)

有人可以帮我解决这个问题,因为程序必须在不同的计算机上运行才能解决这个问题。顺便说一下程序的目标框架是.NET 3.5

1 个答案:

答案 0 :(得分:1)

  
      
  • 确保您的服务器名称正确无误,例如名称上没有拼写错误。
  •   
  • 确保您的实例名称正确,并且目标计算机上确实存在此类实例。 [更新:一些应用程序   将\转换为。如果您不确定您的申请,   请尝试使用Server \ Instance和Server \ Instance   连接字符串]
  •   
  • 确保服务器计算机可以访问,例如,DNS可以正确解析,您可以ping服务器(并非总是如此)。
  •   
  • 确保SQL Browser服务正在服务器上运行。如果在服务器上启用了防火墙,则需要输入sqlbrowser.exe和/或UDP   端口1434异常。
  •   

这似乎是一个很好的参考: http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx

相关问题