Click here to Skip to main content
14,739,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to connect the local database where locate at c drive. how create connection staring and access database


when I tried, this error occurred

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: An attempt to attach an auto-named database for file C:\aInven\Pos_V1\DatabasePos.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

What I have tried:

string connString = "Data Source=.\\TEW_SQLEXPRESS;AttachDbFilename=C:\\aInven\\Pos_V1\\DatabasePos.mdf;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
SqlConnection cnn;

SqlConnection sqlConn = new SqlConnection(connString);
sqlConn.Open();
Posted
Updated 7-Jun-20 8:55am
Comments
Simon_Whale 5-Jun-20 7:22am
   
adding to the solution by OriginalGriff below I would also use https://www.connectionstrings.com as reference material
F-ES Sitecore 5-Jun-20 7:59am
   
You can't use the Sql* classes to connect to Access, they are only for Sql Server. Google how to use Access from c#, lots of examples out there.

Start by checking your connection string - this may help: Simple SQL Connection String Creation[^]
When you have that, things should be smoother.

But ... two things.
1) Never hardcode connection strings - they change, particularly when you release software. They should be stored in a config file of some sort - the way I do it is like this: Instance Storage - A Simple Way to Share Configuration Data among Applications[^] - it's probably overkill for your app, but ...

2) Don't use Attach to connect a DB - create and populate the DB in SQL via SSMS. Attaching is a special debug mode of Sql Server that is only available in Express versions, and is not going to work in production. If you want to use an Access DB directly, then don't use SQL Server at all - use the Ace DB engine, and access the file via Odbc instead.
   
you can take help from the following site for creating connecting connection string.
SQL Server connection strings - ConnectionStrings.com[^] .

can you check your database file Name?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900