Imports System.Data.OleDb Public Class Form3 Dim con As New OleDbConnection Dim adp As New OleDbDataAdapter Dim ds As New DataSet Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load con.ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=students" Dim selectcmd As New OleDbCommand selectcmd.CommandText = "select * from student" selectcmd.Connection = con adp.SelectCommand = selectcmd adp.Fill(ds, "student") End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click For Each txt As TextBox In Me.Controls.OfType(Of TextBox)() txt.Text = "" Next For Each combo As ComboBox In Me.Controls.OfType(Of ComboBox)() combo.Text = "" Next End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim insecmd As New OleDbCommand Dim Vs As String = TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "'," & TextBox3.Text & ",'" & ComboBox3.Text & ComboBox4.Text & ComboBox5.Text & "','" & TextBox4.Text & "','" & ComboBox6.Text & "','" & TextBox5.Text & "')" insecmd.CommandText = "insert into student values('" & Vs insecmd.Connection = con adp.InsertCommand = insecmd adp.Update(ds, "student") Me.Close() End Sub End Class
时间: 2024-04-27 07:22:32 浏览: 57
这是一个使用 OleDb 连接 SQL Server 数据库的 Windows 窗体应用程序的代码。在 Form3_Load 事件中,首先设置了数据库连接字符串,然后创建一个 OleDbCommand 对象并设置其 CommandText 和 Connection 属性,再创建一个 OleDbDataAdapter 对象并设置其 SelectCommand 属性,最后使用 Fill 方法将查询结果填充到 DataSet 对象中。
在 Button1_Click 事件中,首先创建一个 OleDbCommand 对象并设置其 CommandText 和 Connection 属性,然后将其设置为 OleDbDataAdapter 对象的 InsertCommand 属性。接着将要插入的数据拼接成一个字符串 Vs,将其作为 CommandText 的一部分,最后使用 Update 方法将数据插入到数据库中。在 Button2_Click 事件中,清空窗体中所有 TextBox 和 ComboBox 控件的文本。
阅读全文