C#连接到本地Microsoft SQL Server 2012数据库

时间:2014-06-03 21:09:16

标签: c# .net visual-studio-2012 sql-server-2012

我无法连接到与Microsoft SQL Server 2012中的C#代码在同一台计算机上运行的本地数据库。

SQL Server实例名称为:MYINSTANCE 数据库名称为:MyDB

我正在运行的代码是:

SqlConnection sqlConnection = null;
              //  MySqlConnection sqlConnection = null;
                try
                {
                    // Open Databse Connection
                    sqlConnection = new SqlConnection("Server=(localdb)\\MYINSTANCE;Database=MyDB;uid=Andy;password=pwd");

                    sqlConnection.Open();
                }  

当此操作运行时,会捕获一个显示以下内容的异常:

   Testing database connection ... [FAILED!]
   -> Database Connection Failed!
   Exception Caught: A network-related or instance-specific error occurred while
 establishing a connection to SQL Server. The server was not found or was not ac
cessible. Verify that the instance name is correct and that SQL Server is config
ured to allow remote connections. (provider: SQL Network Interfaces, error: 50 -
 Local Database Runtime error occurred. The specified LocalDB instance does not
exist.
)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternal
ConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Bool
ean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFa
ilover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSn
iOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirect
edUserInstance, SqlConnectionString connectionOptions, SqlCredential credential,
 TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTime
r timeout, SqlConnectionString connectionOptions, SqlCredential credential, Stri
ng newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent
ity identity, SqlConnectionString connectionOptions, SqlCredential credential, O
bject providerInfo, String newPassword, SecureString newSecurePassword, Boolean
redirectedUserInstance, SqlConnectionString userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp
tions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConn
ectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn
ectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbCon
nectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions
 userOptions)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOp
tions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co
nnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon
nectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
 owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions
, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection
 outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1
retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()

Server Management Studio中的数据库设置如下所示:

enter image description here

有人可以帮我理解我在这里缺少的东西吗?

3 个答案:

答案 0 :(得分:2)

因此,您可能会混淆系统连接LocalDb和完整的SQL Server安装。您的连接字符串应如下所示:

Data Source=.\MYINSTANCE;Initial Catalog=MyDB;User Id=Andy; Password=pwd; 

如果您的连接失败,并显示“用户登录失败'YOURPC \ Andy'等消息,那么它正在尝试使用集成安全性。您现在可以通过指定:

来强制它
Integrated Security=false

如果这不起作用,请尝试打开集成安全性,而不是指定用户名/密码。这将使用您的Windows凭据进行连接:

Data Source=.\MYINSTANCE;Initial Catalog=MyDB;Integrated Security=true;

答案 1 :(得分:0)

尝试定义localdb的版本,例如:

(LocalDB)\v11.0

答案 2 :(得分:0)

我对

感觉更舒服
sqlConnection = new SqlConnection("Data Source=.\\MYINSTANCE; Initial Cataog=MyDB; uid=Andy;password=pwd");

让我知道这是否有效