views:

212

answers:

1

Is it possible to access the Data Connection added to the project in the code?

I know that it is possible to access DataSets created using visual designer but I would like to access the connection only.

Edit:

It is possible to add Data Connection to the project in VS2008 via Tools > Connect to Database.

I would like to access this connection as an object in my code so that I would not have to specify connection string by myself.

+1  A: 

I don't think you can directly use the connection created by the IDE as if it were one of your project's settings. However, the IDE provides similar functionality through the project properties.

How you get to the properties varies slightly on which language/default configuration you are using. But in general you should be able to try these steps and see if they provide the functionality you wanted:

  1. Open your project.
  2. Find your project in the Solution Explorer and select the project.
  3. On the Project menu, choose the 'ProjectName' Properties menu item.
  4. The project properties tab should open. Select the "Settings" blade.
  5. If you have no settings you will be given the option to create a settings file. Do so if necessary.
  6. In the grid that is displayed, choose the first blank row and type "ConnectionString" into the name field.
  7. In the type drop-down, choose (Connection String)
  8. Click in the value field. When you activate this field, a little expansion button labeled "..." should appear. Click it.
  9. This will bring up a Connection settings dialog box.
  10. Choose your sever from the available drop-down.
  11. Configure your log on credentials to your liking.
  12. Select your database from the available drop-down.
  13. Test connection. If it is OK, then Click OK twice to return to the settings page.
  14. Save your settings.

Now you should be able to access this connection string from your project code.

VB.Net:

Dim cs = My.Settings.ConnectionString

C#:

var cs = Properties.Settings.Default.ConnectionString;
magnifico