开发者

How to use Data Connection created in Visual Studio in code?

开发者 https://www.devze.com 2022-12-21 05:25 出处:网络
Is it possible to access the Data Connection added to the project in the code? I know that it is possible to access DataSets cre开发者_如何学运维ated using visual designer but I would like to access

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

I know that it is possible to access DataSets cre开发者_如何学运维ated 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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号