C# 远程连接SQL Server2005代码[转]

来源:互联网 发布:中国网络封闭到何时 编辑:程序博客网 时间:2024/06/07 03:32

C#连接SQL Server2005的代码语句:

 

strcon = strcon + @"Data Source=" + strcons[0];
strcon = strcon + "," + strcons[2] + ";";
strcon = strcon + "Network Library=" + strcons[1] + ";";
strcon = strcon + "Initial Catalog=" + strcons[3] + ";";
strcon = strcon + "User ID=" + strcons[4] + ";";
strcon = strcon + "Password=" + strcons[5] + ";";
strcon = strcon + "Persist Security Info=True";

 

strcons[0] 服务器名称,一般添机器的IP
strcons[1]协议DBMSSOCN(为tcp/ip协议)
strcons[2]]端口号,一般为1433
strcons[3] 数据库名
strcons[4] 用户名
strcons[5]密码
 

端口号也要配置一下:

在控制面板里的服务和应用程序中的SQL Server配置管理中的SQL Server 2005网络配置内的SQL

Server2005的协议TCP/IP默认为已禁用,在它的属性设置它的端口号为1433 “确定” 启动。

 

网上看到的,没有试过!