使用VB.NET执行存储过程

时间:2011-12-29 04:17:07

标签: vb.net tsql stored-procedures ado.net sql-insert

这是我的程序

ALTER PROCEDURE sp_addUser
     @UserName nvarchar(50),    
     @Prenom nvarchar(50),  
     @Nom nvarchar(50), 
     @Mail nvarchar(50),    
     @Password char(8), 
     @Addresse nvarchar(100),   
     @Ville nvarchar(50),   
     @Province nvarchar(50),    
     @PostalCode char(6),
     @Pays nvarchar(50),    
     @AnimalGenre nvarchar(50), 
     @NomAnimal nvarchar(50),   
     @Race nvarchar(50) 
AS
BEGIN
   INSERT INTO Client
   VALUES (@UserName,@Prenom,@Nom,@Mail,@Password,@Addresse,@Ville,@Province,@PostalCode,@Pays,@AnimalGenre,@NomAnimal,@Race);
END 

我认为存储过程

没问题

现在在数据库中添加值的代码

Sub sp_addUser()

    Dim intRowsAff As Integer

    lblErrMsg.Text = ""
    lblRecsAff.Text = ""

    Dim connectionString As String = WebConfigurationManager.ConnectionStrings("BecsEtMuseauxSQL").ConnectionString
    Dim con As SqlConnection = New SqlConnection(connectionString)

    Dim cmd As New SqlCommand("sp_addUser", con)

    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = txtUserName.Text
    cmd.Parameters.Add("@Prenom", SqlDbType.VarChar, 50).Value = txtPrenom.Text
    cmd.Parameters.Add("@Nom", SqlDbType.NVarChar, 50).Value = txtNom.Text
    cmd.Parameters.Add("@Mail", SqlDbType.NVarChar, 50).Value = txtMail.Text
    cmd.Parameters.Add("@Password", SqlDbType.Char, 8).Value = txtPass.Text
    cmd.Parameters.Add("@Addresse", SqlDbType.NVarChar, 100).Value = txtAdresse.Text
    cmd.Parameters.Add("@Ville", SqlDbType.NVarChar, 50).Value = txtVille.Text
    cmd.Parameters.Add("@Province", SqlDbType.NVarChar, 50).Value = txtProvince.Text
    cmd.Parameters.Add("@PostalCode", SqlDbType.Char, 6).Value = txtPostal.Text
    cmd.Parameters.Add("@Pays", SqlDbType.NVarChar, 50).Value = txtPays.Text
    cmd.Parameters.Add("@AnimalGenre", SqlDbType.NVarChar, 50).Value = rblAnimal.Text
    cmd.Parameters.Add("@NomAnimal", SqlDbType.NVarChar, 50).Value = txtAnimal.Text
    cmd.Parameters.Add("@Race", SqlDbType.NVarChar, 50).Value = txtRace.Text

    Try
        cmd.Connection.Open()
        intRowsAff = cmd.ExecuteNonQuery()
    Catch ex As Exception
        lblErrMsg.Text = ex.Message & ex.Source
    End Try
    lblRecsAff.Text = intRowsAff & " record(s) inserted"
    cmd.Connection.Close()
End Sub

执行方法之后VS说我必须为程序sp_addUser指定很多参数

我不明白为什么会产生错误!

3 个答案:

答案 0 :(得分:3)

只需将Sp与代码隐藏方法同步,并将参数设置为sp。

Dim dt As New DataTable
    Dim sqlpr1 As New List(Of SqlParameter)

    Dim cmd As New SqlCommand()

    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "[sp_ItemPackingList]"
    cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = dateDate.Text.Trim()
    cmd.Parameters.Add("@Thk", SqlDbType.VarChar).Value = txtPendQty.Text.Trim()
    cmd.Parameters.Add("@Dia", SqlDbType.VarChar).Value = txtPendQty.Text.Trim()
    Try
        cmd.ExecuteNonQuery()
        lblMessage.Text = "Record inserted successfully"
    Catch ex As Exception
        Throw ex
        obj.GetDataTable("[sp_ItemPackingList]", sqlpr1)

    End Try

答案 1 :(得分:2)

您发布的代码和存储过程似乎是同步的。

这强烈暗示数据库中的存储过程与您在代码中的预期不同步。建议你检查你发布的内容实际上是DB中的存储过程。

另请注意:您不应在proc前加上'sp_',除非它自然位于master中。

答案 2 :(得分:1)

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
   With con
       .ConnectionString = sqlConnectionString
       .Open()
   End With
   With cmd
       .CommandText = "UpdateProcedure"
       .CommandType = CommandType.StoredProcedure
       .Parameters.AddWithValue("CName", txtCName.Text)
       .Parameters(0).SqlDbType = SqlDbType.NVarChar
      .Parameters.AddWithValue("CAddress",txtCAddress1.Text)
       .Parameters(1).SqlDbType = SqlDbType.NVarChar
       .Parameters.AddWithValue("CCity", txtCCity)
       .Parameters(2).SqlDbType = SqlDbType.NVarChar
       .ExecuteNonSqlQuery()
    End With
Catch ex As Exception

Finally
  cmd.Dispose()
  con.Close()
End Try