Ado recordset con Vb.Net




Module Module1
' http://support.microsoft.com/kb/315974/it

Sub Main()
Dim cn As New ADODB.Connection()
cn.ConnectionString = "provider=sqloledb;server=(localhost);database=northwind;uid=<username>"
cn.Open()

Dim rs As New ADODB.RecordSet()
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
rs.Open("select * from products", cn)
rs.ActiveConnection = Nothing
cn.Close()

Dim da As New System.Data.OleDb.OleDbDataAdapter()
Dim ds As New DataSet()
da.Fill(ds, rs, "products")

Console.Write("There are " & ds.Tables(0).Rows.Count.ToString & " total products.")
Console.ReadLine()
End Sub

End Module





strConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=prova.mdb" & ";Persist Security Info=False"
'strConn.Open()

strSQL = "SELECT * FROM tbl1"
ds = New DataSet("tbl1")
da = New OleDb.OleDbDataAdapter(strSQL, strConn)
da.Fill(ds, "tbl1")
bs = New BindingSource()
bs.DataSource = ds
bs.DataMember = "tbl1"
' strConn.Close()


ListBox1.DataSource = ds.Tables("tbl1")
ListBox1.DisplayMember = "Cognome"
ListBox1.DataBindings.Add("Text", bs, "Cognome")

*************************************************************


'' http://vb.net-informations.com/dataset/dataset-oledb.htm

Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As OleDbConnection
Dim oledbAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim i As Integer

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
sql = "Your SQL Statement Here"

connection = New OleDbConnection(connetionString)
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(sql, connection)
oledbAdapter.Fill(ds)
oledbAdapter.Dispose()
connection.Close()

For i = 0 To ds.Tables(0).Rows.Count - 1
MsgBox(ds.Tables(0).Rows(i).Item(0) & " -- " & ds.Tables(0).Rows(i).Item(1))
Next
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
**********************************************************************************
' http://vb.net-informations.com/dataset/dataset-merge-tables-oledb.htm


Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As OleDbConnection
Dim oledbAdapter As OleDbDataAdapter
Dim ds1 As New DataSet
Dim ds2 As New DataSet
Dim dt As DataTable
Dim firstSql As String
Dim secondSql As String
Dim i As Integer
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
firstSql = "Your First SQL Statement Here"
secondSql = "Your Second SQL Statement Here"
connection = New OleDbConnection(connetionString)
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(firstSql, connection)
oledbAdapter.Fill(ds1, "First Table")
oledbAdapter.SelectCommand.CommandText = secondSql
oledbAdapter.Fill(ds2, "Second Table")
oledbAdapter.Dispose()
connection.Close()

ds1.Tables(0).Merge(ds2.Tables(0))
dt = ds1.Tables(0)

For i = 0 To dt.Rows.Count - 1
MsgBox(dt.Rows(i).Item(0) & " -- " & dt.Rows(i).Item(1))
Next

Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

''''*****************************************************************************+

''''' http://support.microsoft.com/kb/301248/it


Imports System
Imports System.Data
Imports System.Data.SqlClient

Module Module1

Sub Main()
Dim sConnectionString As String
' Modify the following code to correctly connect to your SQL Server.

sConnectionString = "Password=StrongPassword;User ID=UserName;" & _
"Initial Catalog=pubs;" & _
"Data Source=(local)"

Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()

' Create an instance of a DataAdapter.

Dim daAuthors As _
New SqlDataAdapter("Select * From Authors", objConn)

' Create an instance of a DataSet, and retrieve data from the Authors table.

Dim dsPubs As New DataSet("Pubs")
daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
daAuthors.Fill(dsPubs, "Authors")

'*****************

'BEGIN ADD CODE

' Create a new instance of a DataTable

Dim tblAuthors As DataTable
tblAuthors = dsPubs.Tables("Authors")

Dim drCurrent As DataRow
' Obtain a new DataRow object from the DataTable.

drCurrent = tblAuthors.NewRow()

' Set the DataRow field values as necessary.

drCurrent("au_id") = "993-21-3427"
drCurrent("au_fname") = "George"
drCurrent("au_lname") = "Johnson"
drCurrent("phone") = "800 226-0752"
drCurrent("address") = "1956 Arlington Pl."
drCurrent("city") = "Winnipeg"
drCurrent("state") = "MB"
drCurrent("contract") = 1

'Pass that new object into the Add method of the DataTable.Rows collection.

tblAuthors.Rows.Add(drCurrent)
MsgBox("Add was successful.")

'END ADD CODE

'*****************

'BEGIN EDIT CODE


drCurrent = tblAuthors.Rows.Find("213-46-8915")
drCurrent.BeginEdit()
drCurrent("phone") = "342" & drCurrent("phone").ToString.Substring(3)
drCurrent.EndEdit()
MsgBox("Record edited successfully")

'END EDIT CODE

'*****************

'BEGIN SEND CHANGES TO SQL SERVER


Dim objCommandBuilder As New SqlCommandBuilder(daAuthors)
daAuthors.Update(dsPubs, "Authors")
MsgBox("SQL Server updated successfully" & chr(13) & "Check Server explorer to see changes")

' END SEND CHANGES TO SQL SERVER

'*****************

'BEGIN DELETE CODE


drCurrent = tblAuthors.Rows.Find("993-21-3427")
drCurrent.Delete()
MsgBox("Record deleted successfully")

'END DELETE CODE

'*****************

' CLEAN UP SQL SERVER

daAuthors.Update(dsPubs, "Authors")
MsgBox("SQL Server updated successfully" & Chr(13) & Chr(13) & "Check Server Explorer to see changes")
End Sub

End Module










( adorecordsetconvbnet.html )- by Paolo Puglisi - Modifica del 17/12/2023