繁体   English   中英

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

[英]C# Connect to local Microsoft SQL Server 2012 Database

我无法连接到与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中的数据库设置如下所示:

在此处输入图片说明

有人可以帮助我了解我在这里缺少什么吗?

因此,您似乎使系统LocalDb连接到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;

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

(LocalDB)\v11.0

我比较舒服

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

让我知道这是否有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM