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, "Table") 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) End Sub End Class这段代码有什么问题
时间: 2024-03-03 09:49:55 浏览: 166
这段代码有几个问题:
1. 连接字符串是针对 SQL Server 的,而不是 Access 数据库。应该使用 Access 数据库的连接字符串,例如:
```
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb"
```
2. 在插入数据时,字符串值应该使用参数化查询,而不是将值直接拼接到 SQL 语句中。这样可以避免 SQL 注入攻击,并且可以自动处理特殊字符的转义。应该改为类似下面的语句:
```
insecmd.CommandText = "insert into student values(?, ?, ?, ?, ?, ?, ?, ?, ?)"
insecmd.Parameters.AddWithValue("param1", TextBox1.Text)
insecmd.Parameters.AddWithValue("param2", TextBox2.Text)
'...
```
3. 在清空表单数据时,应该检查控件的类型是否为 TextBox 或 ComboBox,否则可能会出现异常。可以改为类似下面的语句:
```
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Text = ""
ElseIf TypeOf ctrl Is ComboBox Then
DirectCast(ctrl, ComboBox).Text = ""
End If
Next
```
4. 在插入数据后应该更新 DataSet 中的数据,以便在下一次查询中可以看到新插入的记录。可以改为类似下面的语句:
```
adp.Update(ds, "Table")
```
根据上述建议,可以将代码修改为:
```
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=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb"
Dim selectcmd As New OleDbCommand
selectcmd.CommandText = "select * from student"
selectcmd.Connection = con
adp.SelectCommand = selectcmd
adp.Fill(ds, "Table")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Text = ""
ElseIf TypeOf ctrl Is ComboBox Then
DirectCast(ctrl, ComboBox).Text = ""
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim insecmd As New OleDbCommand
insecmd.CommandText = "insert into student values(?, ?, ?, ?, ?, ?, ?, ?, ?)"
insecmd.Parameters.AddWithValue("param1", TextBox1.Text)
insecmd.Parameters.AddWithValue("param2", TextBox2.Text)
insecmd.Parameters.AddWithValue("param3", ComboBox1.Text)
insecmd.Parameters.AddWithValue("param4", ComboBox2.Text)
insecmd.Parameters.AddWithValue("param5", TextBox3.Text)
insecmd.Parameters.AddWithValue("param6", ComboBox3.Text & ComboBox4.Text & ComboBox5.Text)
insecmd.Parameters.AddWithValue("param7", TextBox4.Text)
insecmd.Parameters.AddWithValue("param8", ComboBox6.Text)
insecmd.Parameters.AddWithValue("param9", TextBox5.Text)
insecmd.Connection = con
adp.InsertCommand = insecmd
adp.Update(ds, "Table")
End Sub
End Class
```
阅读全文