Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 設計工具產生的程式碼 "
Public Sub New()
MyBase.New()
'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()
'在 InitializeComponent() 呼叫之後加入所有的初始設定
End Sub
'Form 覆寫 Dispose 以清除元件清單。
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 Form 設計工具的必要項
Private components As System.ComponentModel.IContainer
'注意: 以下為 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
<System.Diagnostics.DebuggerStepThrough()> Private 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)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'返回記錄集
' 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()
Dim cm As New SqlClient.SqlCommand("sp_studentQuery", cn)
Dim ada As New SqlClient.SqlDataAdapter
Dim trm As SqlClient.SqlTransaction
cm.CommandType = CommandType.StoredProcedure
cm.Parameters.Add("@studentID", SqlDbType.BigInt).Value = 1
'Dim sqlParams() As SqlClient.SqlParameter = {New SqlParameter("@studentID", 10)}
'cm.Parameters.Add(sqlParams(0))
MsgBox(cm.Parameters.Count().ToString())
' cm.ExecuteReader()
ada.SelectCommand = cm
Dim dataset As New Data.DataTable
ada.Fill(dataset)
Me.DataGrid1.DataSource = dataset
' cm.Transaction = trm
Try
MsgBox(cm.Parameters.Count().ToString() + cm.ExecuteReader.FieldCount.ToString)
' trm.Commit()
Catch ex As Exception
' trm.Rollback()
End Try
' MsgBox(cn.State.ToString)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'返回值
Dim cn As New SqlClient.SqlConnection
cn.ConnectionString = Me.SqlConnection1.ConnectionString
cn.Open()
Dim cm As New SqlClient.SqlCommand("sp_add", cn)
Dim trm As SqlClient.SqlTransaction
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
cm.ExecuteNonQuery()
Dim s As Object = cm.Parameters("@c").Value
MsgBox(CType(s, String))
End Sub
End Class
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 設計工具產生的程式碼 "
Public Sub New()
MyBase.New()
'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()
'在 InitializeComponent() 呼叫之後加入所有的初始設定
End Sub
'Form 覆寫 Dispose 以清除元件清單。
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 Form 設計工具的必要項
Private components As System.ComponentModel.IContainer
'注意: 以下為 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
<System.Diagnostics.DebuggerStepThrough()> Private 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)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'返回記錄集
' 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()
Dim cm As New SqlClient.SqlCommand("sp_studentQuery", cn)
Dim ada As New SqlClient.SqlDataAdapter
Dim trm As SqlClient.SqlTransaction
cm.CommandType = CommandType.StoredProcedure
cm.Parameters.Add("@studentID", SqlDbType.BigInt).Value = 1
'Dim sqlParams() As SqlClient.SqlParameter = {New SqlParameter("@studentID", 10)}
'cm.Parameters.Add(sqlParams(0))
MsgBox(cm.Parameters.Count().ToString())
' cm.ExecuteReader()
ada.SelectCommand = cm
Dim dataset As New Data.DataTable
ada.Fill(dataset)
Me.DataGrid1.DataSource = dataset
' cm.Transaction = trm
Try
MsgBox(cm.Parameters.Count().ToString() + cm.ExecuteReader.FieldCount.ToString)
' trm.Commit()
Catch ex As Exception
' trm.Rollback()
End Try
' MsgBox(cn.State.ToString)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'返回值
Dim cn As New SqlClient.SqlConnection
cn.ConnectionString = Me.SqlConnection1.ConnectionString
cn.Open()
Dim cm As New SqlClient.SqlCommand("sp_add", cn)
Dim trm As SqlClient.SqlTransaction
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
cm.ExecuteNonQuery()
Dim s As Object = cm.Parameters("@c").Value
MsgBox(CType(s, String))
End Sub
End Class