0

来自 dao vb 背景的自学 ado.net。

搜索各种代码示例,我能够编写一些东西来从 sql 中的 pubs 数据库中提取一些数据。现在我正在尝试修改它以满足我的需要。因为我会经常访问数据库,所以我想避免一遍又一遍地重新创建我的对象(sqlconnection、sqldataadapter、dataset),而只在全局范围内创建一次。但是它们似乎被设计为在需要使用它们时使用它们的构造函数来连续构建它们。我能够找到解决大部分问题的方法,但我被困在几个地方。

  1. 如何将其连接SqlDataAdapterSqlConnection其构造函数的外部?
  2. 在结果中引用特定“记录”而不使用 FOR EACH 遍历结果的语法是什么。

这是我的代码(这段代码只是为了学习如何使用它。它不是一个实际的应用程序)

Imports System.Data.SqlClient

Public Class SqlLink

Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet

Public Function Connect(ByVal sConnString As String) As Integer
    'attempts to connect to database, returns error code
    Try

        conn.ConnectionString = sConnString
        conn.Open()
        Return 0

    Catch ex As Exception
        MsgBox(Err.Description)
        Return Err.Number

    End Try

End Function


Public Sub exampleroutine()

    Dim TempRow As DataRow

    'DO NOT WANT Dim da as new sqlDataAdapter("some query", conn)
    'I DO NOT WANT TO DIM THE da HERE. I WANT IT DIM'D ONCE GLOBALLY
    'HOW CAN I TIE THE da TO THE conn HERE OUTSIDE OF IT'S CONSTRUCTOR

    da.SelectCommand.CommandText = "SELECT au_fname FROM authors where au_fname like '%ann%'"
    da.Fill(ds)

    For Each TempRow In ds.Tables(0).Rows
        MsgBox(TempRow("au_fname"))
    Next

    da.SelectCommand.CommandText = "SELECT au_fname FROM authors where au_fname like '%reg%'"
    da.Fill(ds)

    'HOW TO GET A SPECIFIC 'RECORD' WITHOUT ITERATING THROUGH WITH A FOR EACH
    'If ds.Tables(0).Rows.Count > 0 Then ???

End Sub

End Class

==================================================== ====

我制定了一个解决方案,但我不知道是否有人愿意或应该根据评论关注它,但是.. 只是为了结束,这就是我为解决这个问题所做的。

在 Connect 函数中,在 conn.Open 之后我添加了
da = New SqlDataAdapter(False, conn)
这创建了数据适配器的可重用全局实例。

4

2 回答 2

2

我希望这就是你要找的:)

我讨厌在不需要的时候打字:

Imports System.Data.SqlClient

Module Module1

Dim con As New SqlConnection
Dim myConString As String = getSQLString()
Dim objcommand As SqlCommand = New SqlCommand
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet

Public Function getSQLString()
    Dim thisstring As String = "Server.... ur server"
    Return thisString
End Function


Public Function GetData(ByVal sqlstringtext As String)

    'Dim cmdText As String = "SELECT EquipList from SiteAuditor where client='" & GLClient & "' and market='" & GLMarket & "' and project='" & GLProject & "'"
    Dim conn As New SqlConnection(getSQLString())
    Dim strQ As String = String.Empty
    strQ = sqlstringtext
    cmd = New SqlCommand(strQ, conn)
    da = New SqlDataAdapter(cmd)
    ds = New DataSet
    da.Fill(ds)

    'we want to do this on close of program 
    'da = Nothing
    'ds = Nothing
    Return Nothing
End Function


'calling function you can do this whenever because we don't have to re-dim anything there being overridden
Sub whatever()

    GetData("Select thisItem from thisTable where thiscondition='thiscondition")
    'first table'firstrow'firstrecord
    Dim specificRecord As String = ds.Tables(0).Rows(0).Item(0).ToString

End Sub

Sub formclosing()
    'we want to do this on close of program  ( the actual syntax for that would be .... 
    '
    'Private Sub Main_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    'da = Nothing
    'ds = Nothing
    'Application.Exit()
    'End Sub
    '
End Sub

End Module

编辑:如果您必须更改 myconstring,您可以通过将其设置为 public btw 或创建自己的子程序来更改/重新调整它

于 2013-04-30T16:17:00.407 回答
1

您可以设置 SelectCommand 的连接:

da.SelectCommand.Connection = conn

获取表中的第一行:

ds.Tables(0).Rows(0)

您可以使用 DataView 进一步过滤 DataSet。在 DefaultView 的 rowFilter 属性中设置条件。过滤器行随后位于 DefaultView 中。

    ds.Tables(0).DefaultView.RowFilter = "<your conditions here>"
于 2013-04-30T16:09:40.127 回答