同济大学SQL数据库实验10

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mysql As String = "insert into sc values('" & Me.TextBox1.Text & "','" &

Me.TextBox2.Text & "'," & Val(Me.TextBox3.Text) & ")"

Dim myconn As New SqlConnection("database=university;data source=(local);integrated security=true;")

Dim mycmd As New SqlCommand(mysql, myconn)

myconn.Open()

Try

mycmd.ExecuteNonQuery()

Catch ex As Exception

MsgBox(ex.Message)

End Try

myconn.Close()

End Sub

End Class

2、Imports System.Data.SqlClient

Public Class Form1

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Dim mysql As String= "select * from sc where snum='"& ComboBox1.SelectedItem.ToString() & "'"

Dim mysql2 As String = " select sname from student where snum='" +

ComboBox1.SelectedItem.ToString() + "'"

Dim myconn As New SqlConnection("database=university;data source=(local);integrated

security=true;")

Dim mycmd As New SqlCommand(mysql, myconn)

Dim mycmd2 As New SqlCommand(mysql2, myconn)

Dim sum As Integer = 0

Dim count As Integer = 0

Dim stname As String

myconn.Open()

stname = mycmd2.ExecuteScalar()

Dim mydatareader As SqlDataReader

mydatareader = mycmd.ExecuteReader()

Do While mydatareader.Read()

sum = sum + mydatareader.Item(2)

count = count + 1

Loop

mydatareader.Close()

myconn.Close()

sum = sum / count

TextBox2.Text = stname

TextBox3.Text = count

TextBox4.Text = sum

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ComboBox1.Focus()

Dim mysql As String = "select * from student"

Dim myconn As New SqlConnection("database=university;data source=(local);integrated security=true;")

Dim mycmd As New SqlCommand(mysql, myconn)

myconn.Open()

Dim mydatareader As SqlDataReader

mydatareader = mycmd.ExecuteReader()

Do While mydatareader.Read()

ComboBox1.Items.Add(mydatareader.Item(0))

Loop

End Sub

End Class

相关文档
最新文档