Entity FrameWork Calling a Insert Stored Procedure With Vb.Net and MySQL Server RRS feed

  • Question

  • I want work with Entity Framework and Stored Procedures. Now i know how work the EF and i know also how work the SP. The problem is join EF and SP in a code. I want call a SP in a VB.NET code but i don't know how. 

    I have this code

    Public cli As CLCliente

      Dim crcontext As New bd_barb_canraw1Entities
                    Dim param1 As MySqlParameter
                    Dim param2 As MySqlParameter
                    Dim param3 As MySqlParameter
                    Dim param4 As MySqlParameter
                    Dim param5 As MySqlParameter
                    Dim param6 As MySqlParameter
                    Dim param7 As MySqlParameter

         param1 = New MySqlParameter("@nomec", cli.nomeC.ToString)

                    param2 = New MySqlParameter("@datanascc", cli.datnascC.ToString)
                    param3 = New MySqlParameter("@contactoc", cli.contactoC.ToString)
                    param4 = New MySqlParameter("@nuitc", cli.nuitC.ToString)
                    param5 = New MySqlParameter("@cuss", cli.cusC.ToString)
                    param6 = New MySqlParameter("@dtrs", cli.dcRC.ToString)
                    param7 = New MySqlParameter("@cusrs", cli.cusCRC.ToString)
                    cli = New CLCliente(.txtcli.Text, InverteData(.dtpNasc.Text), .txtcontacto.Text, .mskNuit.Text, 100, Now(), 100)
                    crcontext.ExecuteStoreCommand("SPgravacliente", {param1, param2, param3, param4, param5, param6, param7})

    What is wrong in this code??? Please help me

    • Moved by Amanda Zhu Tuesday, June 4, 2013 2:59 AM move to a more appropriate forum
    Saturday, June 1, 2013 8:49 PM

All replies

  • Try the code below

    Imports System.Data.SqlClient
    Module Module1
        Public cli As CLCliente
        Sub Main()
            Dim server As String = "Server"
            Dim database As String = "Databae_Name"
            Dim Username As String = "User"
            Dim Password As String = "Password"
            Dim connStr As String = string.format("Server={0};Database={1};Uid={3};Pwd={3};", & _
               server, database, username,password)
            Dim conn As SqlConnection = New SqlConnection(connStr)
            Dim crcontext As SqlCommand = New SqlCommand("SPgravacliente", conn)
            crcontext.CommandType = CommandType.StoredProcedure
            crcontext.Parameters.AddWithValue("@nomec", cli.nomeC.ToString)
            crcontext.Parameters.AddWithValue("@datanascc", cli.datnascC.ToString)
            crcontext.Parameters.AddWithValue("@contactoc", cli.contactoC.ToString)
            crcontext.Parameters.AddWithValue("@nuitc", cli.nuitC.ToString)
            crcontext.Parameters.AddWithValue("@cuss", cli.cusC.ToString)
            crcontext.Parameters.AddWithValue("@dtrs", cli.dcRC.ToString)
            crcontext.Parameters.AddWithValue("@cusrs", cli.cusCRC.ToString)
        End Sub
    End Module


    Sunday, June 2, 2013 12:31 AM
  • Joel Engineer thank you for the help but by this way i did. Let me explain better. I am doing a small  experience using ADO.Net with (SP, dynamic SQL in code and EF) to do the same thing, Insert, update and delete. I am analyzing the performance of three methods. The result is that the three methods have a similar performance inserting data, so i want now mix the two methods (EF & SP) calling a SP by EF for insert a data in the database MYSQL server. That is the point how can i do that??? 
    Sunday, June 2, 2013 9:41 AM
  • I don't know a lot about EF, but all it is going to do is to build the same command parameters as the SQL client object will do.  What may halp you inderstand what is happening is if you use a sniffer like filddler or wireshark and trace the commands that are sent use the SQL client method.  What you will see is the command list will be sent inside of HTML tags and will look like this

    {parameter1:abc, parametr2:def,parameter3:ghi}


    Sunday, June 2, 2013 10:48 AM