How to use data connection created in Visual Studio in code?

Is it possible to access a data connection added to a project in code?

I know that you can access DataSets created using the visual constructor, but I would like to access the connection only.

Edit:

You can add a data connection to the project in VS2008 using Tools> Connect To Database.

I would like to access this connection as an object in my code, so that I will not need to specify the connection string myself.

+4
source share
1 answer

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

As you get to the properties, you change a little what language configuration / default you use. But in general, you should be able to try these steps and see if they provide the necessary functions:

  • Open the project.
  • Locate the project in Solution Explorer and select the project.
  • In the "Project" menu, select the "Project Properties" menu item.
  • The project properties tab opens. Select the "Settings" blade.
  • If you do not have settings, you will be given the opportunity to create a settings file. Do this if necessary.
  • In the displayed grid, select the first blank line and enter "ConnectionString" in the name field.
  • Select "Connection String" from the drop-down list.
  • Click in the value box. When you activate this field, a small extension button should appear with the inscription "...". Click on it.
  • The Connection Settings dialog box appears.
  • Select a selection from the drop-down list.
  • Set up your login credentials as you wish.
  • Select your database from the drop-down list.
  • Check connection. If everything is ok, click "OK" twice to return to the settings page.
  • Save the settings.

You should now be able to access this connection string from the project code.

VB.Net:

Dim cs = My.Settings.ConnectionString 

FROM#:

 var cs = Properties.Settings.Default.ConnectionString; 
+6
source

Source: https://habr.com/ru/post/1302577/


All Articles