[英]My connectionstring is erroring on its own generated connection string. how would i fix this?
first time working with DB/SQL in Visual Studios 2017 So i created my own Database in with a table inside of it Now i want my code to connect to the DB and i used the generated connectionString but now i am getting errors like :System.Data.SqlClient.SqlException 第一次在Visual Studios 2017中使用DB / SQL,所以我在其中创建了自己的数据库,并在其中包含一个表。现在,我希望我的代码连接到DB,并使用生成的connectionString,但是现在出现了:System之类的错误。 Data.SqlClient.SqlException
That something on my connection OPEN is going wrong 我的连接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 旧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
New Stacktrace with SqlException en ex.ToString(); 带有SqlException的新Stacktrace 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
The relevant part is: 相关部分是:
SqlException (0x80131904): An attempt to attach an auto-named database for file C:\\Users\\xflak\\OneDrive\\Documenten\\GitHub\\ItemDatabase - UI\\WindowsFormsApp2\\Database1.mdf failed.
SqlException(0x80131904):尝试为文件C:\\ Users \\ xflak \\ OneDrive \\ Documenten \\ GitHub \\ ItemDatabase-UI \\ WindowsFormsApp2 \\ Database1.mdf附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。
尝试不使用AttachDbFilename
并在初始目录中放入数据库名称。
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB; Initial Catalog=Your database name ; Integrated Security = True")
I guess, you need to have the .mdf file in the accessible drive like D:/ or E:/ and try to have it in a folder without spaces. 我猜想,您需要将.mdf文件放在可访问的驱动器(如D:/或E:/)中,并尝试将其放在没有空格的文件夹中。
I see you have a path having multiple spaces in it . 我看到您的路径中有多个空格。 ('ItemDatabase - UI' )
(“ ItemDatabase-UI”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.