How to use Data Connection created in Visual Studio in code?
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:
- Open your project.
- Find your project in the Solution Explorer and select the project.
- On the Project menu, choose the 'ProjectName' Properties menu item.
- The project properties tab should open. Select the "Settings" blade.
- If you have no settings you will be given the option to create a settings file. Do so if necessary.
- In the grid that is displayed, choose the first blank row and type "ConnectionString" into the name field.
- In the type drop-down, choose (Connection String)
- Click in the value field. When you activate this field, a little expansion button labeled "..." should appear. Click it.
- This will bring up a Connection settings dialog box.
- Choose your sever from the available drop-down.
- Configure your log on credentials to your liking.
- Select your database from the available drop-down.
- Test connection. If it is OK, then Click OK twice to return to the settings page.
- 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;
精彩评论