i wanna to attach a Database from a dynamic path to a MSSQL server by coding a project to do this ,, what is the code i should write and will it be a Windows Application or Console Application ,, or there is no difference ??
-
2Not enough information. What kind of dynamic path? What database?Oded– Oded2011-03-01 21:30:24 +00:00Commented Mar 1, 2011 at 21:30
-
possible duplicate of connecting to sql server through a .net winform applicationJYelton– JYelton2011-03-01 21:33:42 +00:00Commented Mar 1, 2011 at 21:33
-
any path the user insert ,, and 2 files database .mdf and .ldfOmneya– Omneya2011-03-01 21:34:09 +00:00Commented Mar 1, 2011 at 21:34
4 Answers
You can use any of the two. Just make sure the files are in a place the SQL Server in question can reach and then attach them with an sql statement.
Like this:
CREATE DATABASE [AdventureWorks] ON
( FILENAME = N’C:\Data\AdventureWorks_Data.mdf’ ),
( FILENAME = N’C:\Data\AdventureWorks_Log.ldf’ )
FOR ATTACH
Comments
In the connection string you can attach a database if the database has not already been attached. To do this in C# you should be able to do the following (this is untested):
SQLConnection conn;
try
{
conn = new SQLConnection(String.Format("Server={0};AttachDbFilename={1};Database=dbname; Trusted_Connection=Yes;", "Server Address", @"Path To Database"));
conn.Open();
conn.Close();
}
catch (Exception ex)
{
throw;
}
finally
{
conn.Dispose();
}
Let me know how you get on.
Regards,
Stu
Comments
If I nderstand your question correctly, you are looking for a way to use a databse which the user will select (not the hard coded one).
Go here and learn about Saving User and Application Settings in WinForms. You will get some ideas.