表适配器与数据集 - 获取数据 [英] Table Adapter vs Dataset - Getting data

查看:63
本文介绍了表适配器与数据集 - 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

几个问题。首先,为什么visual studio在创建新的表适配器时有时只生成这种类型的代码?

Couple of questions. first, why does visual studio only generate this type of code sometimes when creating a new table adapter?

  Me.TblMaterialsTableAdapter.Fill(Me.Materials11DataSet.tblMaterials)

 Me.TblMaterialsTableAdapter.Fill(Me.Materials11DataSet.tblMaterials)

我的主要问题:

使用连接到Access数据库的表单中的表适配器,我需要将数据作为可以使用的列表获取而不是在datagrid上使用tableadapter等。一旦我填充了适配器,是否有一个特定的方法,或者最好通过代码创建我自己的
新数据集并填充它以便我可以使用List(of)

Using a table adapter in my form connected to an Access Database, i need to get the data out as a list that i can use and not use the tableadapter on a datagrid etc. is there a specific method once i have filled the adapter or is it best to create my own new dataset via code and populate it so that i can use List(of)

谢谢

Nacho

Nacho是奈杰尔的衍生物 - 真实的事实!我是VB.Net的自学者。 50%的时间,我100%正确!

Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

推荐答案

使用TableAdapter的事情一般都不了解它们说一个开发人员会对他们感到沮丧,同时了解他们你可能会或可能不会在某个时间点受挫。

The thing with using a TableAdapter is without understanding them generally speaking a developer will become frustrated with them while understanding them you may or may not get frustrated at some point in time.

基础知识,Visual Studio创建代表表格的类您的数据库提供开箱即用的Fill方法来获取数据。如果你不研究这段代码,挫折感就会大吃一惊。然后启示,你通过选择
来研究代码,从Visual Studio的Project菜单中显示所有文件,看看有很多代码可能令人望而生畏。好吧,如果您打算使用它们,您需要深入研究代码或使用OleDb数据提供程序
连接和命令对象的不同路径。

The basics, Visual Studio creates classes that represent the table(s) in your database that provide out of the box the Fill method to obtain data. If you don't study this code frustration abounds to no end. Then enlightenment, you study the code by selecting show all files from the Project menu in Visual Studio and see there is a great deal of code that can be daunting to review. Well if you are going to use them you need to dig in and study the code or go a different route which is to use OleDb data provider connection and command object.

向下获取列表

为了您的问题,我们想要一个列表但不想要任何默认行为,例如使用DataGridView或其他控件链接到您的TableAdapter。

Let's say for sake of your question you want a list but don't want any default behavior such as using a DataGridView or other controls linked to your TableAdapter.

将一个字段拖到表单上,对于此示例,我使用了Description。

Drag one field onto the form, for this example I used Description.

这会在我立即删除的表单上放置一个标签TextBox和BindingNavigator。  我在表单上放置两个ListBox控件以进行演示,向您展示如何以不同的方式填充,但我真的不需要ListBox控件作为基础
代码使用for-each或Lambda允许我获得list。

This places a label, TextBox and BindingNavigator on the form which I promptly remove.  I place two ListBox controls on the form for demonstration to show you how to populate both differently yet I really don't need ListBox controls as the underlying code using either a for-each or Lambda allow me to get a list.

在Fill方法下,我写入IDE输出窗口,BindingSource的DataSource和DataMember的类型。

Under the Fill method I write to the IDE output window, the type for the DataSource and DataMember of the BindingSource.

Me.CategoriesTableAdapter.Fill(Me.NorthWind_1DataSet.Categories)
Console.WriteLine(CategoriesBindingSource.DataSource.GetType)
Console.WriteLine(CategoriesBindingSource.DataMember)

输出

接下来我给你两个(还有更多)获得单个字段的方法。

Next I give you two (and there are more) way to get at a single field.

Public Class Form1
    Private Sub CategoriesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.CategoriesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.NorthWind_1DataSet)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'NorthWind_1DataSet.Categories' table. You can move, or remove it, as needed.
        Me.CategoriesTableAdapter.Fill(Me.NorthWind_1DataSet.Categories)
        Console.WriteLine(CategoriesBindingSource.DataSource.GetType)
        Console.WriteLine(CategoriesBindingSource.DataMember)

        For Each row As DataRow In CType(CategoriesBindingSource.DataSource, DataSet).Tables(CategoriesBindingSource.DataMember).Rows
            ListBox1.Items.Add(row.Field(Of String)("CategoryName"))
        Next

        Dim CategoryNames As List(Of String) = CType(CategoriesBindingSource.DataSource, DataSet) _
            .Tables(CategoriesBindingSource.DataMember) _
            .AsEnumerable _
            .Select(Function(row) row.Field(Of String)("CategoryName")) _
            .ToList

        ListBox2.DataSource = CategoryNames
    End Sub
End Class

结果

何这很有助于使用TableAdapter。现在没有TableAdapter我们需要设置连接字符串,如下所示,因为为TableAdapter创建的连接字符串使用了一个可以替换的变量,但同样容易
忽略它。

Hopefully this is of assistance for using a TableAdapter. Now without a TableAdapter we need to set the connection string as shown below because the connection string created for the TableAdapter uses a variable in it which can be replaced but just as easy to ignore it.

Private Builder As New OleDbConnectionStringBuilder With
{
    .Provider = "Microsoft.ACE.OLEDB.12.0",
    .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NorthWind_1.accdb")
}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim categoryList As New List(Of String)
    Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
        Using cmd As New OleDbCommand With {.CommandText = "SELECT CategoryName FROM Categories", .Connection = cn}
            cn.Open()
            Dim reader As OleDbDataReader = cmd.ExecuteReader
            If reader.HasRows Then
                While reader.Read
                    categoryList.Add(reader.GetString(0))
                End While
            End If
        End Using
    End Using

    For Each category As String In categoryList
        Console.WriteLine(category)
    Next
End Sub

对于记录,要更改DataDirectory(如从app.config获取的最后一张图片)你可以设置DataDirectory,如下所示(如果没有使用连接字符串'原样'将失败)

For the record, to change DataDirectory (as per last image taken from app.config) you can set the DataDirectory as shown here (if not using the connection string 'as is' will fail)

AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory)

要检查之前(将为空/无)或之后(将按照设置进行检查)上面)

To examine it before (will be null/nothing) or after (will be set as per above)

AppDomain.CurrentDomain.GetData("DataDirectory").ToString

在我的如果它返回

C:\Development\VS2015 \SimpleDataAdapterSelectAndUpdate\bin\Debug \

C:\Development\VS2015\SimpleDataAdapterSelectAndUpdate\bin\Debug\





这篇关于表适配器与数据集 - 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆