简体   繁体   English

我的连接字符串在其自己生成的连接字符串上出错。 我该如何解决?

[英]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.

相关问题 WCF和EF - 如何强制主机使用自己的连接字符串 - WCF and EF - How to force host to use its own connection string 一台服务器上许多项目的ConnectionString管理,我应该为我们的代码创建自己的ConnectionString提供程序 - ConnectionString management for many projects on one server, should I make my own ConnectionString provider for our code 如何将自己的数据库与连接字符串中的(local)\\ netsdk关联? - How do I associate my own database with (local)\netsdk in the connection string? 我将如何解析自己的XML ..? - How would I parse my own XML..? 我将如何编写自己的FirstOrDefaultAsync()方法 - How would I write my own FirstOrDefaultAsync() method 如何修复“短语”不能为空字符串。 参数名称:短语'? - how to fix ''phrase' cannot be an empty string. Parameter name: phrase'? 我的.NET API以字符串形式返回JSON。 我该如何退货? - My .NET API returns JSON as a String. How do I make it return? 我的项目的一部分开始使用不正确的连接字符串-如何解决该问题? - Part of my project started using incorrect connection string - how do I fix that? 我该怎么做才能修复我的SQLIte连接字符串 - What can i do to fix my SQLIte connection string 我什么时候可以使用自己的RouteHandler? - When would I use my own RouteHandler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM