Imports System.Data
Imports System.Data.SqlClient Public Class Form1 Dim C As Integer = 1000 Dim ds As New DataSet() ' Create a table; set its initial capacity. Dim DataTabl As New DataTable("Employees") Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load DataTabl.MinimumCapacity = 200 ' Create all columns. ' You can create a DataColumn and then add it to the Columns collection. Dim DataCol As New DataColumn("FirstName", GetType(String)) DataTabl.Columns.Add(DataCol) ' Or you can create an implicit DataColumn with the Columns.Add method. DataTabl.Columns.Add("LastName", GetType(String)) DataTabl.Columns.Add("BirthDate", GetType(Date)) ' When you have to set additional properties, you can use an explicit ' DataColumn object, or you can use a With block. With DataTabl.Columns.Add("Address", GetType(String)) .MaxLength = 100 End With ' (When you must set only one property, you can be more concise, ' even though the result isn’t very readable.) DataTabl.Columns.Add("City", GetType(String)).MaxLength = 20 ' Create a calculated column by setting the Expression ' property or passing it as the third argument to the Add method. 'DataTabl.Columns.Add("CompleteName", GetType(String), _ '"FirstName + ’ ’ + LastName") ' Create an identity, auto-incremented column. Dim dcEmpId As New DataColumn("NoID", GetType(Integer)) dcEmpId.AutoIncrement = True ' Make it auto-increment. dcEmpId.AutoIncrementSeed = 1 dcEmpId.AllowDBNull = False ' Default is True. dcEmpId.Unique = True ' All key columns should be unique. DataTabl.Columns.Add(dcEmpId) ' Add to Columns collection. ' Make it the primary key. (Create the array on-the-fly.) DataTabl.PrimaryKey = New DataColumn() {dcEmpId} ' This is a foreign key, but we haven’t created the other table yet. DataTabl.Columns.Add("EmployeeID", GetType(Integer)) ' Add the DataTable to the DataSet. ds.Tables.Add(DataTabl) DataGridView1.DataSource = DataTabl End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim DataTabl As DataTable = ds.Tables("Employees") ' Create a new row with the same schema. Dim dr As DataRow = DataTabl.NewRow() ' Set all the columns. dr("FirstName") = "Joe " & C dr("LastName") = "Doe " & C dr("BirthDate") = #1/1/1955# dr("Address") = "1234 A Street" dr("City") = "Los Angeles" dr("EmployeeID") = C C = C + 1 ' Add to the Rows collection. DataTabl.Rows.Add(dr) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim cn As New SqlConnection("Data Source=GENIO\ISTANZAPAOLOPUGL;Initial Catalog=Northwind;Integrated Security=True") cn.Open() Dim Table As DataTable = ds.Tables(0) Dim Row As DataRow Dim I As Integer Dim Sql As String = "" Dim cmd As New SqlCommand() 'assegno la connessione al comando cmd.Connection = cn For I = 1 To Table.Rows.Count - 1 Row = ds.Tables(0).Rows(I) Sql = "Insert Into Employees " Sql = Sql & " (FirstName, LastName, BirthDate, Address, City) VALUES (" 'Sql = Sql & " (FirstName, LastName, Address, City) VALUES (" Sql = Sql & "'" & Row(0).ToString & "', " Sql = Sql & "'" & Row(1).ToString & "', " Sql = Sql & "" & Row(2) & ", " ' data Sql = Sql & "'" & Row(3).ToString & "', " Sql = Sql & "'" & Row(4).ToString & "') " Debug.Print(Sql) cmd.CommandText = Sql cmd.ExecuteNonQuery() Next MsgBox("OK", MsgBoxStyle.Exclamation, "OK") cmd.Cancel() cn.Close() End Sub End Class |