ComboBox.SelectedText 属性 和数据库错误
ComboBox.SelectedText Property and Database Error
此特定代码 ComboBox2.SelectedItem
查询对我的数据库有错误。我想我在这段代码中遗漏了一些东西 ComboBox2.SelectedItem
:
Private Sub UpdateCombo()
ComboBox2.Items.Clear()
SQLcon.Open()
Dim Command As SqlClient.SqlCommand = SQLcon.CreateCommand()
Command.CommandText = "Select productName From tblProductsStocks"
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
While SQLReader.Read()
ComboBox2.Items.Add(SQLReader.Item("productName"))
End While
SQLcon.Close()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
SQLcon.Open()
Dim Command As SqlClient.SqlCommand = SQLcon.CreateCommand()
Command.CommandText = "Select * From tblProductsStocks WHERE productName=" & ComboBox2.SelectedItem
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
SQLReader.Read()
TextBox1.Text = SQLReader.Item("productType")
TextBox2.Text = SQLReader.Item("productMass")
SQLcon.Close()
End Sub
请启用 Option Strict。这是一个两部分的过程。首先针对当前项目 - 在解决方案资源管理器中双击我的项目。选择左侧的编译。在 Option Strict 下拉菜单中 select ON。第二个用于未来的项目 - 转到工具菜单 -> 选项 -> 项目和解决方案 -> VB 默认值。在 Option Strict 下拉菜单中 select ON。这将使您在运行时避免错误。
连接需要被释放和关闭才能返回到连接池。如果出现错误,您的代码甚至可能无法关闭连接。如果将数据库对象保存在本地,则可以控制它们的关闭和处置。 Using...End Using
块会为您处理此问题,即使出现错误也是如此。在我的代码中,Command 是 Using 块的一部分。注意连接构造函数后的逗号。
您可以将连接字符串直接传递给连接的构造函数。同样将命令文本和连接传递给命令构造函数。
使用参数。它不仅避免了连接字符串的错误,而且还避免了 Sql 注入。在您的代码中, selected 项目是一个字符串,但您没有添加周围的单引号。使用参数时不需要这样做。命令文本是服务器的可执行代码,恶意用户可以输入会破坏数据库的内容。参数被服务器视为值,而不是可执行代码,因此它们更安全。
在最后可能的时刻打开连接,就在.Execute...之前。连接是宝贵的资源,需要尽快打开、关闭和处理。只要 reader 处于启用状态,连接就必须打开。所以我将更新用户界面(文本框)移到了 using 块之外。
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim String1 As String = ""
Dim String2 As String = ""
Using SQLcon As New SqlConnection("Your connection string"),
Command As New SqlCommand("Select * From tblProductsStocks WHERE productName= @producName", SQLcon)
'Check your database for the actual datatype and field size
Command.Parameters.Add("@productName", SqlDbType.VarChar, 100).Value = ComboBox2.SelectedItem.ToString
SQLcon.Open()
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
SQLReader.Read()
String1 = SQLReader.Item("productType").ToString
String2 = SQLReader.Item("productMass").ToString
End Using 'closes and disposes the connection and command
TextBox1.Text = String1
TextBox2.Text = String2
End Sub
此特定代码 ComboBox2.SelectedItem
查询对我的数据库有错误。我想我在这段代码中遗漏了一些东西 ComboBox2.SelectedItem
:
Private Sub UpdateCombo()
ComboBox2.Items.Clear()
SQLcon.Open()
Dim Command As SqlClient.SqlCommand = SQLcon.CreateCommand()
Command.CommandText = "Select productName From tblProductsStocks"
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
While SQLReader.Read()
ComboBox2.Items.Add(SQLReader.Item("productName"))
End While
SQLcon.Close()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
SQLcon.Open()
Dim Command As SqlClient.SqlCommand = SQLcon.CreateCommand()
Command.CommandText = "Select * From tblProductsStocks WHERE productName=" & ComboBox2.SelectedItem
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
SQLReader.Read()
TextBox1.Text = SQLReader.Item("productType")
TextBox2.Text = SQLReader.Item("productMass")
SQLcon.Close()
End Sub
请启用 Option Strict。这是一个两部分的过程。首先针对当前项目 - 在解决方案资源管理器中双击我的项目。选择左侧的编译。在 Option Strict 下拉菜单中 select ON。第二个用于未来的项目 - 转到工具菜单 -> 选项 -> 项目和解决方案 -> VB 默认值。在 Option Strict 下拉菜单中 select ON。这将使您在运行时避免错误。
连接需要被释放和关闭才能返回到连接池。如果出现错误,您的代码甚至可能无法关闭连接。如果将数据库对象保存在本地,则可以控制它们的关闭和处置。 Using...End Using
块会为您处理此问题,即使出现错误也是如此。在我的代码中,Command 是 Using 块的一部分。注意连接构造函数后的逗号。
您可以将连接字符串直接传递给连接的构造函数。同样将命令文本和连接传递给命令构造函数。
使用参数。它不仅避免了连接字符串的错误,而且还避免了 Sql 注入。在您的代码中, selected 项目是一个字符串,但您没有添加周围的单引号。使用参数时不需要这样做。命令文本是服务器的可执行代码,恶意用户可以输入会破坏数据库的内容。参数被服务器视为值,而不是可执行代码,因此它们更安全。
在最后可能的时刻打开连接,就在.Execute...之前。连接是宝贵的资源,需要尽快打开、关闭和处理。只要 reader 处于启用状态,连接就必须打开。所以我将更新用户界面(文本框)移到了 using 块之外。
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim String1 As String = ""
Dim String2 As String = ""
Using SQLcon As New SqlConnection("Your connection string"),
Command As New SqlCommand("Select * From tblProductsStocks WHERE productName= @producName", SQLcon)
'Check your database for the actual datatype and field size
Command.Parameters.Add("@productName", SqlDbType.VarChar, 100).Value = ComboBox2.SelectedItem.ToString
SQLcon.Open()
Dim SQLReader As SqlClient.SqlDataReader = Command.ExecuteReader()
SQLReader.Read()
String1 = SQLReader.Item("productType").ToString
String2 = SQLReader.Item("productMass").ToString
End Using 'closes and disposes the connection and command
TextBox1.Text = String1
TextBox2.Text = String2
End Sub