We have developed a VB.NET application with SQL Server as the back end.
Currently, the .mdf
file is deployed along with the other files required.
Installation of the application is done and the database file (.mdf
) is being placed and used from the local system. The application runs well in the local system.
Now the user needs the application to run in a multi-user system (server and client). As we know, to run the application in client system, the .mdf
should be attached to the SQL Server. Attaching the .mdf
to the server should be done programmatically and SQL Server Management Studio should not be used.
How can this be done?
Connect to the database using known approaches. like this
execute CREATE DATABASE
command with appropriate parameters. reference
your final code should be something like this in C#
var conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = ....
conn.Open();
var command = SqlCommand("CREATE DATABASE DBName ON ( FILENAME = '" + @"C:\DBName.mdf" + "' ), ( FILENAME = '" + @"C:\DBName_log.ldf" + "' ) FOR ATTACH", conn);
com.ExecuteScalar();