Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,
I have an existing data base and I want it to be automatically attached to my program, no matter during the installation process or when the user runs my project. I just want it to be done(attached automatically)

I'm using C#.NET and the name of my database is "MyLibraryDataBase".

I think I should put some code in the main page of my project in the event of form_load

,thanks ;)
Posted
Updated 26-Aug-11 0:08am
v2

if i'm reading this correctly, you are wanting the software to only connect to one database?

Why not set up the database name as a textbox field where the text defaults to "MyLibraryDataBase", pass that to a variable that is then used in your code to connect to your database, that way you can always change it at a later date if you need to but will always default to "MyLibraryDataBase" if nothing is specified?
 
Share this answer
 
Comments
hrhm_04 26-Aug-11 14:43pm    
Yes, This is what I want! and after the attach, my project can search the name of books in the database with the codes i wrote in a SqlDataAdapter.
you can set the connection string in your config-file. You can use that to connect to a database. In my article Disadvantages of SqlParameters Turned into Advantages[^] you can find a method that finds your defaultconnection in your config file
 
Share this answer
 
I think everyone's missing the question here a bit correct me if im wrong but are you looking for publishing the program to multiple PC's and then each instance of the program has its own database?

try:

http://msdn.microsoft.com/en-us/library/ms228283(v=vs.80).aspx[^]
 
Share this answer
 
v2
Comments
hrhm_04 26-Aug-11 14:45pm    
Yes that right! I dont want to attach the database on every pc manually!
Hello Friends, I think I found the answer and here it is. I wrote this code in the event of clicking a button named attach database:


private void BTNAttach_Click(object sender, EventArgs e)
       {

           try
           {
               SqlConnection con = new SqlConnection();
               con.ConnectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
               con.Open();

               string str = "use master;" +
              "EXECUTE sp_attach_db @dbname = N'MyLibraryDataBase' , " +
                " @filename1 = N'" + System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase.mdf'," +
                "@filename2 = N'" + System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase_log.ldf'";

               LAMDFSQLAdress.Text = System.Environment.CurrentDirectory+"\\Data\\MyLibraryDataBase.mdf";
               LALOGSQLAdress.Text = System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase_log.ldf";



               SqlCommand cmd = new SqlCommand(str, con);
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("DataBase Successfully Attached.");
           }

           catch (Exception x)
           {
               if (x.Message.IndexOf("already exists") >= 0)
                   MessageBox.Show("DataBase already exists.");
               else
                   MessageBox.Show(x.Message);
           }

       }
 
Share this answer
 
v2
You only need to connect to it when you want to retrieve or add/insert data. Other than those two instances, the connection shouldn't be open.

EDIT ====================

If I understand your comment, you're talking about the actual Visual Studio project? Unless your database is on a remote computer that is also accessible by the other system, this isn't possible unless you export the database and give them a SQL script they can run to build/update their own copy (there are also other considerations that I probably won't remember all of).
 
Share this answer
 
v2
Comments
hrhm_04 25-Aug-11 19:57pm    
Hi,
I know what you say:) but this is not my problem.
I want my database to attach automatically to my project when the user runs the project, you know, when I install it on another pc, the database isn't attached and the project cant use it.

thanks John ;)
walterhevedeich 25-Aug-11 20:30pm    
So, do you mean, each application that will be installed will have its own database? Or do you mean, you want the application to have a connection to a central database when you install it? Which is which?
hrhm_04 26-Aug-11 14:50pm    
you know, i give a sql database with the program. in my pc the database is attached.But in other pc's it is not, I think it is because my sql adress is different from others. i found my answer. Take a look at the code i put.Thanks ;)
Ok, so there is still no answer on this..
I'm also wondering how to do this!
 
Share this answer
 
Comments
hrhm_04 26-Aug-11 14:55pm    
Take a look at the code I put.

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