大网数据广告活动
数据库与应用安全

Use server name parameter in a connection string

· 52CE 我爱测

This article describes how to programmatically specify the client network library in the connection string when you connect to a SQL Server database.

Summary

This article describes how to programmatically specify the client network library in the connection string when you connect to a SQL Server database.

In Microsoft Data Access Components (MDAC) 2.6 and later, you can specify the client access library by using the server name parameter in connection string. Therefore, you can specify a specific client access library when you are prompted by an application for a server name to which to connect. This behavior can be useful when you are testing and troubleshooting connectivity issues for SQL Server.

For example, you can use the Osql command-line utility to connect to SQL Server and to force it to use the TCP/IP network library:

osql -Stcp:myServer,portNumber -E

Original product version:   SQL Server
Original KB number:   313295

Code Sample

The following Microsoft Visual C# .NET code sample demonstrates how to set the connection string. The connection string has the same format irrespective of the language that you use:

using System;
using System.Data;
using System.Data.SqlClient;

namespace getCurrentProtocol
{
    /// <summary>
    /// Main Application Driver Class
    /// </summary>
    class Driver
    {
        static void Main(string[] args)
        {
            string sCxn = "server=myServer;Integrated Security=SSPI; database=master";
            //string sCxn = "server=np:myServer;Integrated Security=SSPI; database=master";
            //string sCxn = "server=tcp:myServer;Integrated Security=SSPI; database=master";
            //string sCxn = "server=rpc:myServer;Integrated Security=SSPI; database=master";
            //string sCxn = "server=lpc:myServer;Integrated Security=SSPI; database=master";
            string sCmd = "SELECT net_library from sysprocesses where spid=@@spid";
            SqlConnection cxn = new SqlConnection(sCxn);
            SqlCommand sqlCmd = new SqlCommand(sCmd, cxn);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sCmd, cxn);
            DataTable dt = new DataTable();
            try 
            {
                sqlDa.Fill(dt);
                Console.WriteLine("Hit ENTER to continue ...");
                Console.ReadLine();
                foreach (DataRow dr in dt.Rows)
                Console.WriteLine(dr["net_library"]);
            } 
            catch (SqlException e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("SQL Error Number: " + e.Number);
                Console.WriteLine("SQL Error Message: " + e.Message);
            }
        }
    }
}

Note: The connection string and particularly the value of the server parameter:

string sCxn = "server=myServer;Integrated Security=SSPI; database=northwind"

Use the Code Sample with Various Network Libraries

The following code samples demonstrate how to use the value of the server parameter to specify various network libraries:

  • TCP/IP:

    server=tcp:hostname
    

    You can optionally specify a specific port number. By default, the port is 1433.

    server=tcp:hostname, portNumber
    
  • Named Pipes:

    server=np:hostname
    

    You can optionally specify a specific named pipe.

    server=np:\\hostname\pipe\pipeName
    

    By default, the pipe name is sql\query. If you connect to a named instance, the pipe name is typically in the following format:

    MSSQL$instnaceName\sql\query

  • The default value of the underlying protocol is determined by the operating system settings where a protocol can have any one of the following values:

    Value Underlying Protocol
    ncacn_np Named Pipes
    ncacn_ip_tcp Transmission Control Protocol/Internet Protocol (TCP/IP)
    ncalrpc Local procedure call
  • Shared Memory:

    server=lpc:hostname
    

References

For more information, see Appendix A: Registry Entries.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 09/25/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。