程序開發中.........
[注明:该Blog中的信息都并非原创,只是作为个人的阅读笔记]
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class Form1Class Form1
Inherits System.Windows.Forms.Form
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Windows Form 設計工具產生的程式碼#Region " Windows Form 設計工具產生的程式碼 "
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
MyBase.New()
![](/Images/OutliningIndicators/InBlock.gif)
'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()
![](/Images/OutliningIndicators/InBlock.gif)
'在 InitializeComponent() 呼叫之後加入所有的初始設定
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
'Form 覆寫 Dispose 以清除元件清單。
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Dispose()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
![](/Images/OutliningIndicators/InBlock.gif)
'為 Windows Form 設計工具的必要項
Private components As System.ComponentModel.IContainer
![](/Images/OutliningIndicators/InBlock.gif)
'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 48)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(440, 176)
Me.DataGrid1.TabIndex = 1
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT studentID, studentName, studentAge, studentDesc FROM Student"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO Student(studentID, studentName, studentAge, studentDesc) VALUES (@stu" & _
"dentID, @studentName, @studentAge, @studentDesc); SELECT studentID, studentName," & _
" studentAge, studentDesc FROM Student"
Me.SqlInsertCommand1.Connection = Me.SqlConnection1
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@studentID", System.Data.SqlDbType.Int, 4, "studentID"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@studentName", System.Data.SqlDbType.VarChar, 10, "studentName"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@studentAge", System.Data.SqlDbType.Int, 4, "studentAge"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@studentDesc", System.Data.SqlDbType.VarChar, 50, "studentDesc"))
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=FYK;packet size=4096;integrated security=SSPI;data source=FYK;pers" & _
"ist security info=True;initial catalog=test"
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Student", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("studentID", "studentID"), New System.Data.Common.DataColumnMapping("studentName", "studentName"), New System.Data.Common.DataColumnMapping("studentAge", "studentAge"), New System.Data.Common.DataColumnMapping("studentDesc", "studentDesc")})})
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "返回值"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(312, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(128, 22)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(456, 229)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
#End Region
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
![](/Images/OutliningIndicators/InBlock.gif)
'返回記錄集
' Dim cn As New SqlClient.SqlConnection("server=.;database=test;password=;user id=sa")
Dim cn As New SqlClient.SqlConnection
cn.ConnectionString = Me.SqlConnection1.ConnectionString
cn.Open()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Dim cm As New SqlClient.SqlCommand("sp_studentQuery", cn)
Dim ada As New SqlClient.SqlDataAdapter
Dim trm As SqlClient.SqlTransaction
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
cm.CommandType = CommandType.StoredProcedure
cm.Parameters.Add("@studentID", SqlDbType.BigInt).Value = 1
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
'Dim sqlParams() As SqlClient.SqlParameter = {New SqlParameter("@studentID", 10)}
![](/Images/OutliningIndicators/InBlock.gif)
'cm.Parameters.Add(sqlParams(0))
MsgBox(cm.Parameters.Count().ToString())
' cm.ExecuteReader()
![](/Images/OutliningIndicators/InBlock.gif)
ada.SelectCommand = cm
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Dim dataset As New Data.DataTable
![](/Images/OutliningIndicators/InBlock.gif)
ada.Fill(dataset)
![](/Images/OutliningIndicators/InBlock.gif)
Me.DataGrid1.DataSource = dataset
![](/Images/OutliningIndicators/InBlock.gif)
' cm.Transaction = trm
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Try
![](/Images/OutliningIndicators/InBlock.gif)
MsgBox(cm.Parameters.Count().ToString() + cm.ExecuteReader.FieldCount.ToString)
' trm.Commit()
Catch ex As Exception
' trm.Rollback()
End Try
![](/Images/OutliningIndicators/InBlock.gif)
' MsgBox(cn.State.ToString)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
![](/Images/OutliningIndicators/InBlock.gif)
'返回值
Dim cn As New SqlClient.SqlConnection
cn.ConnectionString = Me.SqlConnection1.ConnectionString
cn.Open()
![](/Images/OutliningIndicators/InBlock.gif)
Dim cm As New SqlClient.SqlCommand("sp_add", cn)
Dim trm As SqlClient.SqlTransaction
![](/Images/OutliningIndicators/InBlock.gif)
Dim ada As New SqlClient.SqlDataAdapter
cm.CommandType = CommandType.StoredProcedure
cm.Parameters.Add("@a", SqlDbType.BigInt).Value = 152
cm.Parameters.Add("@b", SqlDbType.BigInt).Value = 11
cm.Parameters.Add("@c", SqlDbType.BigInt).Direction = ParameterDirection.Output
![](/Images/OutliningIndicators/InBlock.gif)
cm.ExecuteNonQuery()
![](/Images/OutliningIndicators/InBlock.gif)
Dim s As Object = cm.Parameters("@c").Value
![](/Images/OutliningIndicators/InBlock.gif)
MsgBox(CType(s, String))
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
End Class
![](/Images/OutliningIndicators/None.gif)