c# parameterized stored procedures

Brant Snow
Brant Snow used Ask the Experts™
on
I have alot of sql in some of my code that i want to switch out to parameterized stored procedures.

I see lots of tutorials out there, just wondering if you have any best practice approachs to recommend, or if there is anything within the entity framework or some microsoft class that would make sense and help.

For example, if you look at this URL  https://msdn.microsoft.com/en-us/library/vstudio/zxsa8hkf(v=vs.100).aspx
Its a tutorial for a stored procedure through managed code

Why would you do this vs just writing it directly to the database?

Anyway best advice or approaches would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carl TawnSenior Systems and Integration Developer
Commented:
You wouldn't generally write a CLR stored procedure unless you specifically needed to. CLR stored procedures allow you to leverage .Net code to do advanced processing that would be difficult, or resource intensive, to do with pure T-SQL.

An assembly will be deployed to the SQL server and registered with a proxy stored procedure that calls in to a method in the assembly.

The link you pointed to is referring to creating and testing a CLR enabled procedure for SQL server, rather than a pure t-sql procedure. I think the fact that it is using an overly simplified example probably doesn't help.
Information Security Specialist
Commented:
hi

here is a sample

 Try

            Dim oSQLConnection1 As System.Data.SqlClient.SqlConnection = (New DBConnection).StartConnection
            Try

                oSQLConnection1.Open()

                Dim cmd As New SqlClient.SqlCommand("Update_Case_Study", oSQLConnection1)
                cmd.CommandType = CommandType.StoredProcedure

                cmd.Parameters.Add(New SqlClient.SqlParameter("@Case_ID", SqlDbType.Int, 0, ParameterDirection.Input)).Value = 1
         
  cmd.Parameters.Add(New SqlClient.SqlParameter("@First_Name", SqlDbType.NVarChar, 30, ParameterDirection.Input)).Value ="First_Name"

   cmd.ExecuteNonQuery()

   Catch ex As Exception
                Return False
            Finally
                Try
                    oSQLConnection1.Close()
                Catch ex As Exception

                End Try
            End Try

        Catch ex As Exception

        End Try

Open in new window

Kamal KhaleefaInformation Security Specialist

Commented:
and here is the connection to db

Public Function StartConnection() As SqlConnection



        Dim con As SqlClient.SqlConnection = Nothing
       

        '   Dim ConnectionString As String = "Data Source=server;Initial Catalog=dbname;Persist Security Info=True;User ID=sa;Password=*****; "
       
       
     


        Try
            con = New SqlClient.SqlConnection(ConnectionString)
         
        Catch ex As Exception
     
 
        End Try
        Return con




    End Function

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial