WinForm accessing local SQL Server database files
1. Add the config configuration to the project as follows:
<Configuration> <connectionStrings> <Addname= "ConnStr"connectionString= "Data source=.; attachdbfilename=| Datadirectory|test1.mdf; User Id=user1; password=123456; Connect timeout=30 "ProviderName= "System.Data.SqlClient"/> </connectionStrings></Configuration>
2. Replace the file path in the configuration file by adding the following code in the main method of the program Master Portal (Program.cs) | datadirectory| values of:
string datadir =if (Datadir.endswith (@ "\bin\debug\") | | Datadir.endswith (@ "\bin\release\")) { = System.IO.Directory.GetParent (DataDir). Parent.Parent.FullName; AppDomain.CurrentDomain.SetData ("DataDirectory", DataDir);}
3. Access the database directly from the connection string in the configuration file.
WinForm accessing local SQL Server database files