简体   繁体   English

在事务中使用SQL“选择”填充数据表

[英]Fill Datatable using SQL 'select' WITHIN A TRANSACTION

I would like to fill a datatable with results from a SQL select statment but using a transaction. 我想用来自SQL select语句但使用事务的结果填充数据表。 The reason that I am using a transaction is because I have a list of names (as a datatable), and I want to iterate through the list of names and select the database rows where the name = the name on the list. 我使用事务的原因是因为我有一个名称列表(作为数据表),并且我想遍历名称列表并选择名称=列表中名称的数据库行。 There are 500,000 names in the database and I only want to retreive the relevant rows. 数据库中有500,000个名称,我只想检索相关的行。 I have the code for the procedure as I think it should look like (untested) BUT I dont know HOW to place the data into a datatable .... so Im missing something where I declare the datatable and the 'fill' of that table , could someone help with this ? 我有该过程的代码,因为我认为它应该看起来像(未经测试的),但是我不知道如何将数据放入数据表中....所以我在声明数据表和该表的“填充”位置缺少内容,有人可以帮忙吗? Or suggest how else I can get the information out of the batabase without looking up each name individually. 或者提出其他建议,如何在不单独查找每个名称的情况下从数据库中获取信息。

 Using connection As New SQLite.SQLiteConnection(R2WconectionString)
            connection.Open()
            Dim sqliteTran As SQLite.SQLiteTransaction = connection.BeginTransaction()
            Try
                oMainQueryR = "SELECT NameID, Address, Ocupation FROM Employees Where Name= :Name"
                Dim cmdSQLite As SQLite.SQLiteCommand = connection.CreateCommand()
                With cmdSQLite
                    .CommandType = CommandType.Text
                    .CommandText = oMainQueryR
                    .Parameters.Add(":Name", SqlDbType.VarChar)
                End With
               'Prevent duplicate selects by using a dictionary
                Dim NameInalready As New Dictionary(Of String, String) 

                For Each row As DataRow In TheLIST.Rows
                    If NameInalready.ContainsKey(row.Item("Name")) Then
                    Else
                        NameInalready.Add(row.Item("Name"), "")
                        cmdSQLite.Parameters(":Name").Value = row.Item("Name")
                        cmdSQLite.ExecuteNonQuery()
                    End If

                Next

                sqliteTran.Commit()

            Catch ex As Exception
            End Try
        End Using

First, you don't need a transaction because you aren't updating the database. 首先,您不需要事务,因为您不需要更新数据库。

Second, depending on the possible number of Names in TheLIST, it might be worthwhile for you to change the name selector to IN (ie SELECT * FROM Employees WHERE Name IN ('name1', 'name2') . However, if you expect more than about 10, this is probably not worth trouble. 其次,根据TheLIST中名称的可能数量,可能值得将名称选择器更改为IN (即SELECT * FROM Employees WHERE Name IN ('name1', 'name2') 。但是,如果您期望更多的选择超过10,这可能不值得麻烦。

Finally, you need to create a new DataTable to hold the results. 最后,您需要创建一个新的DataTable来保存结果。 Then you need to create a DataAdapter passing cmdSqlLite as the constructor parameter. 然后,您需要创建一个将cmdSqlLite作为构造函数参数传递的DataAdapter。 And finally, replace your ExecuteNonQuery with DataAdapter.Fill(DataTable). 最后,将您的ExecuteNonQuery替换为DataAdapter.Fill(DataTable)。

For example (after Dim cmdSQLite ): 例如(在Dim cmdSQLite ):

Dim oDataTable As New DataTable("Employees")
Dim oAdapter As New SqliteDataAdapter(cmdSQLite)

and replacing the ExecuteNonQuery line with: 并将ExecuteNonQuery行替换为:

oAdapter.Fill(oDataTable)

I will qualify this code by saying it may need some tweaks. 我将通过说可能需要一些调整来限定此代码。 I only work with class objects and collections, so my preference would have actually been to load a collection of Employee class instances. 我仅使用类对象和集合,因此我的偏好实际上是加载Employee类实例的集合。

I would have done that by replacing ExecuteNonQuery with ExecuteReader and then the loading the read data into a new class instance. 通过将ExecuteNonQuery替换为ExecuteReader,然后将读取的数据加载到新的类实例中,可以做到这一点。 This type of approach resolves various issues with serializing the data across service boundaries (ie Xml for web services) and also lets you embed business logic, if needed, into the classes. 这种类型的方法解决了跨服务边界(即Web服务的Xml)对数据进行序列化的各种问题,并且还允许您根据需要将业务逻辑嵌入到类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM