0

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 ??

3
  • 2
    Not enough information. What kind of dynamic path? What database? Commented Mar 1, 2011 at 21:30
  • possible duplicate of connecting to sql server through a .net winform application Commented Mar 1, 2011 at 21:33
  • any path the user insert ,, and 2 files database .mdf and .ldf Commented Mar 1, 2011 at 21:34

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

0

Are you talking about using System.Data.SqlConnection class?

You can dynamically build your connectionString when you create your SqlConnection.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.