如何在VB.NET中从数据源查询/获取数据

发布于 2024-12-13 11:06:36 字数 483 浏览 3 评论 0原文

我正在 vb.net 中构建一个数据库应用程序,我首先从工具栏中的 DATA 添加数据源。我的连接良好,它在数据源面板中显示了我的所有表。

我还看到与我的数据库相关的新课程,例如

sakilaDataSet
sakilaDataSet.customerDataTable

...... 等等。

我如何查询和使用这些?我用谷歌搜索了很多,但我无法得到这个。

Dim cust As sakilaDataSet.customerDataTable = New sakilaDataSet.customerDataTable
        Dim row() As System.Data.DataRow = cust.Select("customer_id=5")
        MsgBox(row.Count)

我的最后一次尝试是使用上面的代码,但 row.count 总是为零。

i am building a database application in vb.net and i started by adding a data source from the DATA in the toolbar. my connection is good and it shows all my tables in the data source panel.

i also see new classes related to my database, like

sakilaDataSet
sakilaDataSet.customerDataTable

...
and so on.

how do i query and use these ? i googled a lot and i am not able to get this.

Dim cust As sakilaDataSet.customerDataTable = New sakilaDataSet.customerDataTable
        Dim row() As System.Data.DataRow = cust.Select("customer_id=5")
        MsgBox(row.Count)

My last try was with the above code, but the row.count always turns out to be zero.

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

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

发布评论

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

评论(3

谜兔 2024-12-20 11:06:36

您需要打开与数据库的连接。以下是一些选项:

您可以使用 EntityFramework,它提供了一种很好的方法通过映射到实体(类)来访问数据并控制它。为此,在VisualStudio中创建一个ClassLibrary项目,添加一项ADO.NET Entity Data Model。这将打开一个向导,帮助您连接到数据库、将数据库中的对象映射到实体并通过对实体上下文的引用来访问实体。基础知识很简单。

另一种选择是使用 OLEDB 提供程序,这是访问 DB 的经典方式。打开员工 Access DB 的示例

Dim connString As String = "provider= microsoft.jet.oledb.4.0; " & _
     "data source=Employee.mdb;"

  Dim conn As New OleDbConnection(connString)

  Try
     conn.Open()
  Finally
     conn.Close()
     Console.WriteLine("Connection Closed")
  End Try

访问http://www.connectionstrings.com/ 获取许多数据库的公共连接字符串列表。其他有用的链接:

EntityFramework:

http://www.codeguru.com/csharp /csharp/net30/article.php/c15489

http://www.asp.net/entity-framework/tutorials

OLEDB:

http://oreilly.com/catalog/progvbdotnet/chapter/ch08.html

http://www.homeandlearn.co.uk/net/nets12p2ed.html

http://www.sourcecodester.com/tutorials/net/database-programming-made-easy.html

希望这会有所帮助。

You need to open a connextion to the DB. Here are some options:

You could use EntityFramework, which provides a nice way to access data and control it by mapping to entities (classes). For this, in VisualStudio create a ClassLibrary project, add an item ADO.NET Entity Data Model. This will open a wizard that will help you connect to the DB, map the objects in the DB to entities and access the entities by a reference to an entity context. The basics are easy.

Other option is to use an OLEDB provider, which is a classic way to access DB. An example to open an Access DB of employees:

Dim connString As String = "provider= microsoft.jet.oledb.4.0; " & _
     "data source=Employee.mdb;"

  Dim conn As New OleDbConnection(connString)

  Try
     conn.Open()
  Finally
     conn.Close()
     Console.WriteLine("Connection Closed")
  End Try

Visit http://www.connectionstrings.com/ to get a list of common connection string for many DB. Other useful links:

EntityFramework:

http://www.codeguru.com/csharp/csharp/net30/article.php/c15489

http://www.asp.net/entity-framework/tutorials

OLEDB:

http://oreilly.com/catalog/progvbdotnet/chapter/ch08.html

http://www.homeandlearn.co.uk/net/nets12p2ed.html

http://www.sourcecodester.com/tutorials/net/database-programming-made-easy.html

Hope this helps.

泛滥成性 2024-12-20 11:06:36

我想要实现的目标是不再使用连接字符串。在VB.net中添加数据源后,它会生成数据类和适配器,我可以直接使用它们来访问数据库,如下所示:

Dim staff As sakilaDataSet.customerDataTable = New sakilaDataSetTableAdapters.customerTableAdapter().GetData
Dim rows() As sakilaDataSet.customerRow = staff.Select("email='" + email.Text + "'")

What i wanted to achieve was not to use connection strings again. After adding the data source in VB.net, it makes Data Classes and Adapters, which i can use directly to access the database, as follows :

Dim staff As sakilaDataSet.customerDataTable = New sakilaDataSetTableAdapters.customerTableAdapter().GetData
Dim rows() As sakilaDataSet.customerRow = staff.Select("email='" + email.Text + "'")
高速公鹿 2024-12-20 11:06:36

本网站 http://visualbasic.about.com/od/usingvbnet/a/begdbapp7 .htm 有一个很好的教程,其中讨论了使用数据源窗口时会发生什么以及之后如何在代码中使用它。

我知道这是一个老问题,但它在谷歌结果中的排名非常高,而且它更像是一个“你应该这样做”的答案,而不是实际回答你看起来问的问题

This website http://visualbasic.about.com/od/usingvbnet/a/begdbapp7.htm had a good tutorial where it talked about what happens when you use the data sources window and how to use it in your code after that.

I know it's an old question, but it was very high in Google results, and it was more of a "This is how you should have done it" answer instead of actually answering with what it seems like you asked

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