впервые работая с DB / SQL в Visual Studios 2017 Итак, я создал свою собственную базу данных с таблицей внутри нее. Теперь я хочу, чтобы мой код подключился к БД, и я использовал сгенерированную connectionString, но теперь я получаю такие ошибки, как: System.Data.SqlClient.SqlException
Что-то в моем соединении OPEN работает неправильно
namespace WindowsFormsApp2
{
class DBConnect
{
public void connectDB()
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\xflak\OneDrive\Documenten\GitHub\ItemDatabase - UI\WindowsFormsApp2\Database1.mdf; Integrated Security = True");
try
{
con.Open();
Console.WriteLine("Connection Open!");
}catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
OLD Stacktrace
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
bij System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
bij System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
bij System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
bij System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
bij System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
bij System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
bij System.Data.SqlClient.SqlConnection.Open()
bij WindowsFormsApp2.DBConnect.connectDB() in C:\Users\xflak\OneDrive\Documenten\GitHub\ItemDatabase-UI\WindowsFormsApp2\DBConnect.cs:regel 17
Новая трассировка стека с SqlException en ex.ToString ();
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
System.Data.SqlClient.SqlException (0x80131904): An attempt to attach an auto-named database for file C:\Users\xflak\OneDrive\Documenten\GitHub\ItemDatabase - UI\WindowsFormsApp2\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
bij System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
bij System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
bij System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
bij System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
bij System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
bij System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
bij System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
bij System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
bij System.Data.SqlClient.SqlConnection.Open()
bij WindowsFormsApp2.DBConnect.connectDB() in C:\Users\xflak\OneDrive\Documenten\GitHub\ItemDatabase-UI\WindowsFormsApp2\DBConnect.cs:regel 17
ClientConnectionId:200efe66-c5b0-4085-acb2-67a1c554ed67
Error Number:15350,State:1,Class:14
Соответствующая часть:
SqlException (0x80131904): попытка присоединить базу данных с автоматическим именем для файла C: \ Users \ xflak \Ошибка OneDrive \ Documenten \ GitHub \ ItemDatabase - UI \ WindowsFormsApp2 \ Database1.mdf.База данных с тем же именем существует, или указанный файл не может быть открыт, или он находится на общем ресурсе UNC.