我正在尝试使用可视化基本表单将3个文本框值插入到我的数据库中,我之前已经在另一个表单上执行此操作并且它正常工作,因此我复制了代码并更改了名称以适应但我得到了下降错误:'Addcustomer'不是'sql'的成员。
任何帮助将不胜感激。
这是我在表单上使用的代码,这是错误以蓝色下划线的地方:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Sql.Addcustomer(addcustomerfname.Text, addcustomersname.Text, addcustomeremail.Text)
MsgBox("Customer Added")
addeditmembership.Show()
Me.Hide()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
好的,我是VB的新手,我只是为了一个单一的任务,所以只是按照讲师的指示。 这是我的控制文件的完整代码(这包括工作插件和有问题的插件),我知道它可能是一些愚蠢的错误,但它让我感到难过。
Public Class SQLControl
Public SQLCon As New SqlConnection With {.ConnectionString = "Data Source=WADE\SQL2012;Initial Catalog=master;Integrated Security=True;"}
Public SQLCmd As SqlCommand
Public SQLDA As SqlDataAdapter
Public SQLDS As DataSet
Public Function HasConnection() As Boolean
Try
SQLCon.Open()
SQLCon.Close()
Return True
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return False
End Function
Public Sub RunQuery(Query As String)
Try
SQLCon.Open()
' CREATE COMMAND
SQLCmd = New SqlCommand(Query, SQLCon)
'Fill Dataset
SQLDA = New SqlDataAdapter(SQLCmd)
SQLDS = New DataSet
SQLDA.Fill(SQLDS)
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message )
'Close connection
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
End Sub
Public Sub Addmember(member_fname As String, member_sname As String, member_gender As String, member_dob As String,
member_address As String, member_postcode As String, member_email As String, member_contact_number As String,
member_registration As String, member_discount_rate As Integer)
Try
Dim strinsert As String = "INSERT INTO members (member_fname,member_sname,member_gender,member_dob,member_address,member_postcode,member_email,member_contact_number,member_registration,member_discount_rate " & _
")VALUES(" & _
"'" & member_fname & "'," & _
"'" & member_sname & "'," & _
"'" & member_gender & "'," & _
"'" & member_dob & "'," & _
"'" & member_address & "'," & _
"'" & member_postcode & "'," & _
"'" & member_email & "'," & _
"'" & member_contact_number & "'," & _
"'" & member_registration & "'," & _
"'" & member_discount_rate & "')"
SQLCon.Open()
SQLCmd = New SqlCommand(strinsert, SQLCon)
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub Addcustomer(addcustomerfname As String, addcustomersname As String, addcustomeremail As String)
Try
Dim customerinsert As String = "INSERT INTO customers (customer_fname,customer_sname,customer_email " & _
")VALUES(" & _
"'" & addcustomerfname & "'," & _
"'" & addcustomersname & "'," & _
"'" & addcustomeremail & "')"
SQLCon.Open()
SQLCmd = New SqlCommand(customerinsert, SQLCon)
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
答案 0 :(得分:1)
您的代码包含几个问题,最不容易发生SQL injection攻击,但解决这个问题却是另一回事。 (一件简单的事情就是参数化你的查询。有关如何执行此操作的示例,请参阅this StackOverflow question。)
关于您的直接问题:Addcustomer
在类SQLControl
内声明。您正在调用Sql.Addcustomer
,这意味着包含Button1_Click
的表单类应该有一个名为Sql
的字段或属性,类型为SQLControl
。如果不是这种情况,那么您必须声明并初始化字段/属性Sql As SQLControl
。