VB.Net by API/System.Data/DataTable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:49, 26 мая 2010
DataTable.Columns
Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
Shared Sub Main()
Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
Dim sql As String = "SELECT * FROM Employee"
Dim thisCommand As New SqlCommand(sql, thisConnection)
Try
thisConnection.Open()
Console.WriteLine("Connection Opened")
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
Dim schema As DataTable = thisReader.GetSchemaTable()
For Each row As DataRow In schema.Rows
For Each col As DataColumn In schema.Columns
Console.WriteLine(col.ColumnName & " = " & row(col).ToString())
Next
Next
thisReader.Close()
Catch ex As SqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
DataTable.Columns.Add
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
public class BindDataTableToControl
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
" The data source.
Private m_ContactsTable As DataTable
" The data source"s CurrencyManager.
Private m_CurrencyManager As CurrencyManager
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
" Make a DataTable.
m_ContactsTable = New DataTable("Contacts")
" Add columns.
m_ContactsTable.Columns.Add("FirstName", GetType(String))
m_ContactsTable.Columns.Add("LastName", GetType(String))
m_ContactsTable.Columns.Add("Street", GetType(String))
m_ContactsTable.Columns.Add("City", GetType(String))
m_ContactsTable.Columns.Add("State", GetType(String))
m_ContactsTable.Columns.Add("Zip", GetType(String))
" Make the combined FirstName/LastName unique.
Dim first_last_columns() As DataColumn = { _
m_ContactsTable.Columns("FirstName"), _
m_ContactsTable.Columns("LastName") _
}
m_ContactsTable.Constraints.Add( _
New UniqueConstraint(first_last_columns))
" Make some contact data.
m_ContactsTable.Rows.Add(New Object() {"A", "B","C", "D", "E", "11111"})
m_ContactsTable.Rows.Add(New Object() {"F", "G","H", "I", "J", "22222"})
m_ContactsTable.Rows.Add(New Object() {"K", "L","M", "N", "O", "33333"})
m_ContactsTable.Rows.Add(New Object() {"P", "Q","R", "S", "T", "44444"})
m_ContactsTable.Rows.Add(New Object() {"U", "V","W", "X", "Y", "55555"})
m_ContactsTable.Rows.Add(New Object() {"Z", "A","B", "C", "D", "66666"})
m_ContactsTable.Rows.Add(New Object() {"E", "F","G", "H", "I", "77777"})
m_ContactsTable.Rows.Add(New Object() {"J", "K","L", "M", "N", "88888"})
" Bind to controls.
txtFirstName.DataBindings.Add("Text", m_ContactsTable, "FirstName")
txtLastName.DataBindings.Add("Text", m_ContactsTable, "LastName")
txtStreet.DataBindings.Add("Text", m_ContactsTable, "Street")
txtCity.DataBindings.Add("Text", m_ContactsTable, "City")
txtState.DataBindings.Add("Text", m_ContactsTable, "State")
txtZip.DataBindings.Add("Text", m_ContactsTable, "Zip")
" Save a reference to the CurrencyManager.
m_CurrencyManager = _
DirectCast(Me.BindingContext(m_ContactsTable), CurrencyManager)
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFirst.Click
m_CurrencyManager.Position = 0
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrev.Click
m_CurrencyManager.Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNext.Click
m_CurrencyManager.Position += 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLast.Click
m_CurrencyManager.Position = m_CurrencyManager.Count - 1
End Sub
" Add a new record.
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
m_CurrencyManager.AddNew()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox("Are you sure you want to delete this record?", _
MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _
"Confirm Delete?") = MsgBoxResult.Yes _
Then
m_CurrencyManager.RemoveAt(m_CurrencyManager.Position)
End If
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label6 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.btnDelete = New System.Windows.Forms.Button
Me.btnAdd = New System.Windows.Forms.Button
Me.btnLast = New System.Windows.Forms.Button
Me.btnNext = New System.Windows.Forms.Button
Me.btnPrev = New System.Windows.Forms.Button
Me.btnFirst = New System.Windows.Forms.Button
Me.txtZip = New System.Windows.Forms.TextBox
Me.txtState = New System.Windows.Forms.TextBox
Me.txtCity = New System.Windows.Forms.TextBox
Me.txtStreet = New System.Windows.Forms.TextBox
Me.txtLastName = New System.Windows.Forms.TextBox
Me.txtFirstName = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
"Label6
"
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(176, 104)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(22, 13)
Me.Label6.TabIndex = 35
Me.Label6.Text = "Zip"
"
"Label5
"
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(8, 104)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(32, 13)
Me.Label5.TabIndex = 34
Me.Label5.Text = "State"
"
"Label4
"
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(8, 80)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(24, 13)
Me.Label4.TabIndex = 33
Me.Label4.Text = "City"
"
"Label3
"
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(8, 56)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(35, 13)
Me.Label3.TabIndex = 32
Me.Label3.Text = "Street"
"
"Label2
"
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(8, 32)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(58, 13)
Me.Label2.TabIndex = 31
Me.Label2.Text = "Last Name"
"
"Label1
"
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(57, 13)
Me.Label1.TabIndex = 30
Me.Label1.Text = "First Name"
"
"btnDelete
"
Me.btnDelete.Location = New System.Drawing.Point(240, 144)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.Size = New System.Drawing.Size(32, 24)
Me.btnDelete.TabIndex = 29
Me.btnDelete.Text = "X"
"
"btnAdd
"
Me.btnAdd.Location = New System.Drawing.Point(200, 144)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(32, 24)
Me.btnAdd.TabIndex = 28
Me.btnAdd.Text = "+"
"
"btnLast
"
Me.btnLast.Location = New System.Drawing.Point(104, 144)
Me.btnLast.Name = "btnLast"
Me.btnLast.Size = New System.Drawing.Size(32, 24)
Me.btnLast.TabIndex = 27
Me.btnLast.Text = ">>"
"
"btnNext
"
Me.btnNext.Location = New System.Drawing.Point(72, 144)
Me.btnNext.Name = "btnNext"
Me.btnNext.Size = New System.Drawing.Size(32, 24)
Me.btnNext.TabIndex = 26
Me.btnNext.Text = ">"
"
"btnPrev
"
Me.btnPrev.Location = New System.Drawing.Point(40, 144)
Me.btnPrev.Name = "btnPrev"
Me.btnPrev.Size = New System.Drawing.Size(32, 24)
Me.btnPrev.TabIndex = 25
Me.btnPrev.Text = "<"
"
"btnFirst
"
Me.btnFirst.Location = New System.Drawing.Point(8, 144)
Me.btnFirst.Name = "btnFirst"
Me.btnFirst.Size = New System.Drawing.Size(32, 24)
Me.btnFirst.TabIndex = 24
Me.btnFirst.Text = "<<"
"
"txtZip
"
Me.txtZip.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtZip.Location = New System.Drawing.Point(200, 104)
Me.txtZip.Name = "txtZip"
Me.txtZip.Size = New System.Drawing.Size(72, 20)
Me.txtZip.TabIndex = 23
"
"txtState
"
Me.txtState.Location = New System.Drawing.Point(72, 104)
Me.txtState.Name = "txtState"
Me.txtState.Size = New System.Drawing.Size(32, 20)
Me.txtState.TabIndex = 22
"
"txtCity
"
Me.txtCity.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtCity.Location = New System.Drawing.Point(72, 80)
Me.txtCity.Name = "txtCity"
Me.txtCity.Size = New System.Drawing.Size(200, 20)
Me.txtCity.TabIndex = 21
"
"txtStreet
"
Me.txtStreet.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtStreet.Location = New System.Drawing.Point(72, 56)
Me.txtStreet.Name = "txtStreet"
Me.txtStreet.Size = New System.Drawing.Size(200, 20)
Me.txtStreet.TabIndex = 20
"
"txtLastName
"
Me.txtLastName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtLastName.Location = New System.Drawing.Point(72, 32)
Me.txtLastName.Name = "txtLastName"
Me.txtLastName.Size = New System.Drawing.Size(200, 20)
Me.txtLastName.TabIndex = 19
"
"txtFirstName
"
Me.txtFirstName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtFirstName.Location = New System.Drawing.Point(72, 8)
Me.txtFirstName.Name = "txtFirstName"
Me.txtFirstName.Size = New System.Drawing.Size(200, 20)
Me.txtFirstName.TabIndex = 18
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(278, 175)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnDelete)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.btnLast)
Me.Controls.Add(Me.btnNext)
Me.Controls.Add(Me.btnPrev)
Me.Controls.Add(Me.btnFirst)
Me.Controls.Add(Me.txtZip)
Me.Controls.Add(Me.txtState)
Me.Controls.Add(Me.txtCity)
Me.Controls.Add(Me.txtStreet)
Me.Controls.Add(Me.txtLastName)
Me.Controls.Add(Me.txtFirstName)
Me.Name = "Form1"
Me.Text = "UseCurrencyManager"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnDelete As System.Windows.Forms.Button
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents btnLast As System.Windows.Forms.Button
Friend WithEvents btnNext As System.Windows.Forms.Button
Friend WithEvents btnPrev As System.Windows.Forms.Button
Friend WithEvents btnFirst As System.Windows.Forms.Button
Friend WithEvents txtZip As System.Windows.Forms.TextBox
Friend WithEvents txtState As System.Windows.Forms.TextBox
Friend WithEvents txtCity As System.Windows.Forms.TextBox
Friend WithEvents txtStreet As System.Windows.Forms.TextBox
Friend WithEvents txtLastName As System.Windows.Forms.TextBox
Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
End Class
DataTable.Rows
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Public Class MainClass
Shared Sub Main()
Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employee.mdb;"
Dim objConnection As New OleDbConnection(strConnectionString)
Dim strSQL As String = "SELECT FirstName, LastName FROM Employee"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("Employee")
Dim objDataRow As DataRow
Try
objConnection.Open()
objDataAdapter.Fill(objDataTable)
For Each objDataRow In objDataTable.Rows
Console.WriteLine(objDataRow.Item("FirstName") & " " & objDataRow.Item("LastName"))
Next
Catch OleDbExceptionErr As OleDbException
Console.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
Console.WriteLine(InvalidOperationExceptionErr.Message)
End Try
objConnection.Close()
objDataRow = Nothing
objDataTable.Dispose()
objDataTable = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing
End Sub
End Class
DataTable.Rows.Add
Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
Shared Sub Main()
Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
" Sql Query
Dim sql As String = "SELECT * FROM Employee"
Try
" Create Data Adapter
Dim da As New SqlDataAdapter
da.SelectCommand = New SqlCommand(sql, thisConnection)
" Create and fill Dataset
Dim ds As New DataSet
da.Fill(ds, "Employee")
" Get the Data Table
Dim dt As DataTable = ds.Tables("Employee")
" Display Rows Before Changed
Console.WriteLine("Before altering the dataset")
For Each row As DataRow In dt.Rows
Console.WriteLine("{0} | {1} | {2}", _
row("ID").ToString().PadRight(10), _
row("FirstName").ToString().PadRight(10), _
row("LastName"))
Next
dt.Columns("FirstName").AllowDBNull = True
dt.Rows(0)("FirstName") = "Birm"
Dim newRow As DataRow = dt.NewRow()
newRow("ID") = "99"
newRow("FirstName") = "Ever"
newRow("LastName") = "Dame"
dt.Rows.Add(newRow)
For Each row As DataRow In dt.Rows
Console.WriteLine("{0} | {1} | {2}", _
row("ID").ToString().PadRight(10), _
row("FirstName").ToString().PadRight(10), _
row("LastName"))
Next
Catch ex As SqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class