Imports System.Data
Imports System.Data.SqlClient
Public Class NorthWind
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents btnQuery As System.Windows.Forms.Button
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents RegionId As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents tbDesc As System.Windows.Forms.TextBox
Friend WithEvents tbId As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnQuery = New System.Windows.Forms.Button()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.btnAdd = New System.Windows.Forms.Button()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.RegionId = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.tbDesc = New System.Windows.Forms.TextBox()
Me.tbId = New System.Windows.Forms.TextBox()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'btnQuery
'
Me.btnQuery.Location = New System.Drawing.Point(8, 16)
Me.btnQuery.Name = "btnQuery"
Me.btnQuery.Size = New System.Drawing.Size(56, 23)
Me.btnQuery.TabIndex = 0
Me.btnQuery.Text = "查询"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(80, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(216, 96)
Me.DataGrid1.TabIndex = 1
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(8, 120)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(56, 23)
Me.btnAdd.TabIndex = 2
Me.btnAdd.Text = "新增"
'
'Panel1
'
Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.tbId, Me.RegionId, Me.Label1, Me.tbDesc})
Me.Panel1.Location = New System.Drawing.Point(88, 120)
Me.Panel1.Name = "Panel1"
Me.Panel1.TabIndex = 3
'
'RegionId
'
Me.RegionId.Location = New System.Drawing.Point(8, 8)
Me.RegionId.Name = "RegionId"
Me.RegionId.Size = New System.Drawing.Size(56, 16)
Me.RegionId.TabIndex = 0
Me.RegionId.Text = "RegionId"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "RegionDesc"
'
'tbDesc
'
Me.tbDesc.Location = New System.Drawing.Point(32, 64)
Me.tbDesc.Name = "tbDesc"
Me.tbDesc.Size = New System.Drawing.Size(136, 20)
Me.tbDesc.TabIndex = 1
Me.tbDesc.Text = ""
'
'tbId
'
Me.tbId.Location = New System.Drawing.Point(80, 8)
Me.tbId.Name = "tbId"
Me.tbId.TabIndex = 1
Me.tbId.Text = ""
'
'NorthWind
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(304, 358)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.btnAdd, Me.DataGrid1, Me.btnQuery})
Me.Name = "NorthWind"
Me.Text = "NorthWind"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'Dim constr As String = "initial catalog=northwind;data source=localhost;Integrated Security=SSPI"
Dim constr As String = "initial catalog=northwind;data source=localhost;user id=sa;password=michal;"
Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
Dim table As New DataTable()
Dim ds As SqlDataAdapter
Dim sql As String = "select * from region"
ds = New SqlDataAdapter(sql, constr)
ds.Fill(table)
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = table
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If tbId.Text = String.Empty Then
MsgBox("RegionId不能为空")
Exit Sub
End If
Try
Integer.Parse(tbId.Text)
Catch
MsgBox("RegionId必须是整形")
Exit Sub
End Try
Dim command As SqlCommand
Dim sql As String = "insert into region values(@regionId,@regionDesc)"
command = New SqlCommand(sql, New SqlConnection(constr))
command.Parameters.Add(New SqlParameter("@regionId", tbId.Text))
command.Parameters.Add(New SqlParameter("@regionDesc", tbDesc.Text))
command.Connection.Open()
Try
command.ExecuteNonQuery()
MsgBox("新增成功")
Catch
MsgBox("新增失败,请检查资料是否重复")
End Try
command.Connection.Close()
End Sub
End Class