JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating a Data Connection in Code

To create a dataset in code in the DataSetCode example on the CD-ROM, and to load the authors table from the pubs database into it, I start by creating a dataset object when the user clicks the "Load data" button in this example (see Figure 22.1):

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim ds As New DataSet()
    ds = New DataSet("authors")
        

Now we'll need a connection object to connect to the authors table, and I create that connection like this, using a connection string (for more on creating connection strings, see the In Depth section of this chapter):

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim ds As New DataSet()
    ds = New DataSet("authors")

    Dim ConnectionString As String = "Provider=SQLOLEDB.1;Integrated " & _
        "Security=SSPI;Persist Security Info=False;Initial " & _
        "Catalog=pubs;Packet Size=4096;Workstation ID=STEVE;" & _
        "Use Encryption for Data=False"

    Dim Connection1 As OleDbConnection = New _
        OleDbConnection(ConnectionString)
        

Next, we'll need a command object. See the next topic.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor