在vb 2008上添加到SQL Server数据库的问题

时间:2013-03-29 15:14:04

标签: sql-server database vb.net

我遇到了问题,但我不确切地知道它的错误,我的代码如下:

Imports System.Data.SqlClient
Imports System.Data
Public Class FormAdd

Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    FormAdmin.Show()
    Me.Hide()
End Sub

Private Sub btnAdd_Click(ByRef Success As String, ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Call AddUser(Success)
    MsgBox(" " & Success & " ")
End Sub
Private Sub AddUser(ByRef Success As String)
    lblAdmin.Text = Str(chkAdminAccount.Checked)
    Dim con As New SqlConnection
    Dim lrd As SqlDataReader
    Dim inscmd As New SqlCommand

    inscmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim())
    txtUsername.Text = txtUsername.Text
    inscmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim)
    txtPassword.Text = txtPassword.Text
    inscmd.Parameters.AddWithValue("@Name", (txtName.Text.Trim))
    txtName.Text = txtName.Text
    inscmd.Parameters.AddWithValue("@AdminAccount", lblAdmin.Text.Trim)
    lblAdmin = lblAdmin
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Alwyn\Desktop\Computing A2 Alwyn\Comp4ProjectRoomBookingSystem\WindowsApplication1\WindowsApplication1\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    Try
        con.Open()
        inscmd.CommandText = "INSERT INTO Admin (Username, Password, Name, AdminAccount) VALUES (@Username, @Password, @Name, @AdminAccount)"
        inscmd.Connection = con
        inscmd.ExecuteNonQuery()

    Catch ex As Exception
        MsgBox("Could not add User" & ex.Message & " ")
    Finally
        con.Close()
        inscmd.Parameters.Clear()
        Success = "User has been added"
    End Try

End Sub

End Class

如果你能提供帮助,我们将非常感谢错误信息如下:

  

方法' Private Sub btnAdd_Click(ByRef Success As String,sender As Object,e As System.EventArgs)'无法处理事件'公共事件点击(发件人为对象,e为System.EventArgs)'因为他们没有兼容的签名。 C:\ Users \ Alwyn \ Desktop \ Computing A2 Alwyn \ Comp4ProjectRoomBookingSystem \ WindowsApplication1 \ WindowsApplication1 \ FormAdd.vb 10 130 WindowsApplication1

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

btnAdd_Click是一个事件处理程序,您无法更改其预定义签名

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles btnAdd.Click
    Dim Success as string
    Call AddUser(Success)
    MsgBox(" " & Success & " ")
End Sub

从声明中删除Success变量,并在事件处理程序中作为局部变量插入。当然,如果您在click事件之外也需要该变量,那么您需要在表单全局级别

声明它

答案 1 :(得分:0)

如果您想通过签名发送成功,您可以随时在签名中进行可选呼叫。 (例如:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs, Optional ByRef Success As String = Nothing ) Handles btnAdd.Click
    If Success IsNot Nothing then 
            Call AddUser(Success)
            MsgBox(" " & Success & " ")
    End If
End Sub

如果您使用可选关键字,并且在发件人和电子邮件之后,您应该对当前的处理方式没有任何问题。