如何在代码中使用在Visual Studio中创建的数据连接?

发布于 2024-08-23 07:56:26 字数 169 浏览 0 评论 0原文

是否可以在代码中访问添加到项目中的数据连接?

我知道可以访问使用可视化设计器创建的数据集,但我只想访问连接。

编辑:

可以通过“工具”>“VS2008”将数据连接添加到项目中连接到数据库。

我想在代码中作为对象访问此连接,这样我就不必自己指定连接字符串。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安稳善良 2024-08-30 07:56:26

我认为您不能直接使用 IDE 创建的连接,就像它是您的项目设置之一一样。但是,IDE 通过项目属性提供类似的功能。

根据您使用的语言/默认配置,获取属性的方式略有不同。但一般来说,您应该能够尝试这些步骤,看看它们是否提供您想要的功能:

  1. 打开您的项目。
  2. 在解决方案资源管理器中找到您的项目并选择该项目。
  3. 在“项目”菜单上,选择“项目名称”属性菜单项。
  4. 项目属性选项卡应该打开。选择“设置”边栏选项卡。
  5. 如果您没有设置,您将可以选择创建设置文件。如有必要,请这样做。
  6. 在显示的网格中,选择第一个空白行并在名称字段中键入“ConnectionString”。
  7. 在类型下拉列表中,选择(连接字符串)
  8. 单击值字段。当您激活此字段时,应该会出现一个标有“...”的小扩展按钮。单击它。
  9. 这将弹出一个连接设置对话框。
  10. 从可用下拉列表中选择您的服务器。
  11. 根据您的喜好配置您的登录凭据。
  12. 从可用下拉列表中选择您的数据库。
  13. 测试连接。如果没问题,则单击“确定”两次,返回设置页面。
  14. 保存您的设置。

现在您应该能够从项目代码访问此连接字符串。

VB.Net:C#

Dim cs = My.Settings.ConnectionString

var cs = Properties.Settings.Default.ConnectionString;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文