C#连接到SQL Server 2012 Express失败 [英] C# Connection to SQL Server 2012 Express is Fail

查看:128
本文介绍了C#连接到SQL Server 2012 Express失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MSDN获取此代码。

此页面 [ ^ ]

I took this code from MSDN.
This page[^]

// compile with: 
// /r:Microsoft.SqlServer.Smo.dll
// /r:Microsoft.SqlServer.ConnectionInfo.dll
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;


public class A
{
    public static void Main()
    {
        String sqlServerLogin = "user_id";
        String password = "pwd";
        String instanceName = "instance_name";
        String remoteSvrName = "remote_server_name";

        // Connecting to an instance of SQL Server using SQL Server Authentication
        Server srv1 = new Server();   // connects to default instance
        srv1.ConnectionContext.LoginSecure = false;   // set to true for Windows Authentication
        srv1.ConnectionContext.Login = sqlServerLogin;
        srv1.ConnectionContext.Password = password;
        Console.WriteLine(srv1.Information.Version);   // connection is established

        // Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
        ServerConnection srvConn = new ServerConnection();
        srvConn.ServerInstance = @".\" + instanceName;   // connects to named instance
        srvConn.LoginSecure = false;   // set to true for Windows Authentication
        srvConn.Login = sqlServerLogin;
        srvConn.Password = password;
        Server srv2 = new Server(srvConn);
        Console.WriteLine(srv2.Information.Version);   // connection is established


        // For remote connection, remote server name / ServerInstance needs to be specified
        ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
        srvConn2.LoginSecure = false;
        srvConn2.Login = sqlServerLogin;
        srvConn2.Password = password;
        Server srv3 = new Server(srvConn2);
        Console.WriteLine(srv3.Information.Version);   // connection is established
    }
}



不幸的是这段代码不起作用。

它说:管理不在Microsoft.SqlServer中。

如何修复它?


Unfortunately this code does not work.
It say: "Management" is not in "Microsoft.SqlServer".
How to fix it?

推荐答案

这是我用于ConnectionString的内容。比您在示例中显示的所有代码简单得多。对于Microsoft.SqlServer.Management.Smo和Microsoft.SqlServer.Management.Common,您不需要使用语句。以下代码适用于本地PC或服务器上的SQL Server Express。



This is what I use for ConnectionString. Much simpler than all of the code you show in your example. You do not need the using statements for Microsoft.SqlServer.Management.Smo and Microsoft.SqlServer.Management.Common. The code below works for SQL Server Express on local PC or on a server.

using System.Data.SqlClient;




public static string cnString=@"Database=MyDatabaseName;Server=MyServerName\SQLEXPRESS;Connect timeout=15;Integrated security=True;Net=dbmssocn;Application name=MyAppName;";
public static SqlConnection srvConn= new SqlConnection(cnString);
srvConn.Open();


这篇关于C#连接到SQL Server 2012 Express失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆