======在窗體中添加DataGrid==========
Sub classquery()
Dim str As String
Dim ds As New DataSet
Dim da As SqlDataAdapter
str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
Dim con As New SqlConnection(str)
con.Open()
Dim sql As String = "select * from student_Info where 教室代碼 = '" & TextBox1.Text.ToString().Trim() & "' "
da = New SqlDataAdapter(sql, con)
da.Fill(ds)
DataGrid1.DataSource = ds.Tables(0)
End Sub
======此方法是檢驗數據是否已存在============
Sub singleYanzheng()
Dim str As String
str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
Dim con As New SqlConnection(str)
con.Open()
Dim sql As String = "select * from depart_Info where 系部ID='" & TextBox1.Text.ToString().Trim() & "' "
Dim cmd As New SqlCommand(sql, con)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader
If reader.Read() = True Then
MsgBox("該部門號已存在")
Else
addDepartInfo()
MsgBox("success")
clear()
End If
End Sub
========執行插入動作===========
Sub addDepartInfo()
Dim str As String
str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
Dim con As New SqlConnection(str)
con.Open()
Dim sql As String = "insert into depart_Info(系部ID,系部名稱,系主任) values('" & TextBox1.Text.ToString().Trim() & "','" & TextBox2.Text.ToString().Trim() & " ','" & TextBox3.Text.ToString().Trim() & "') "
Dim cmd As New SqlCommand(sql, con)
Try
cmd.ExecuteNonQuery() '執行SQL語句
Catch e As Exception
Console.WriteLine(e.Message) '無法執行時提示出錯信息
End Try
Console.WriteLine("Record Added")
End Sub