Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. store procedure or sql command

store procedure or sql command

Scheduled Pinned Locked Moved C#
databasecsharphelpquestionannouncement
16 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Keith Barrow

    Using a stored procedure is generally considered better. The SQL Server can create an execution plan which speeds the execution up. You also get built in protection against SQL injection attacks. Whatever you do don't use an SQL string statement like this (without parameters):

    string sql = "Select * from Table where name = " + idTextBox.Text;

    as a hacker can type in SQL to the text box idTextBox and manipulate your DB, this is called SQL injection. Because Stored Procedures use parameters anyway, they are protected against this. Finally, I'd suggest looking at the following: LinqToSql, The Entity Framework, NHibernate. These map C# objects to relational databases, you will find that this removes the need for a lot of boilerplate code. It looks like you have a new development, so using these technologies will probably be better, but YMMV.

    Sort of a cross between Lawrence of Arabia and Dilbert.[^]

    S Offline
    S Offline
    sa alavifar
    wrote on last edited by
    #4

    string sql = "Select * from Table where name = " + idTextBox.Text; when i have an update, i was using the parameter that they are protected against SQL injection i asked this question for example initial datagrid view in form laod, so what is your idea?

    P OriginalGriffO 2 Replies Last reply
    0
    • S sa alavifar

      string sql = "Select * from Table where name = " + idTextBox.Text; when i have an update, i was using the parameter that they are protected against SQL injection i asked this question for example initial datagrid view in form laod, so what is your idea?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #5

      This is still wide open to a SQL Injection attack. Just because it's a select statement, doesn't stop it from having a SQL Injection attack attached to it.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • S sa alavifar

        string sql = "Select * from Table where name = " + idTextBox.Text; when i have an update, i was using the parameter that they are protected against SQL injection i asked this question for example initial datagrid view in form laod, so what is your idea?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #6

        Try this:

        idTextBox.Text = "Jones;DROP TABLE Table";
        string sql = "Select * from Table where name = " + idTextBox.Text;

        Or better still, don't try it - just think about it. Can the user type that into idTextBox? Oh yes! And what happens to your database? X|

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        K 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Try this:

          idTextBox.Text = "Jones;DROP TABLE Table";
          string sql = "Select * from Table where name = " + idTextBox.Text;

          Or better still, don't try it - just think about it. Can the user type that into idTextBox? Oh yes! And what happens to your database? X|

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #7

          I have a confession to make, sometimes I open your profile just to see the picture :)

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]

          OriginalGriffO 1 Reply Last reply
          0
          • K Keith Barrow

            I have a confession to make, sometimes I open your profile just to see the picture :)

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            :laugh: I'm not sure whether to be flattered that you like it, or disturbed that you felt a need to say it! :-D

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            K 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              :laugh: I'm not sure whether to be flattered that you like it, or disturbed that you felt a need to say it! :-D

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #9

              That's exactly the effect I was looking for :)

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]

              OriginalGriffO 1 Reply Last reply
              0
              • K Keith Barrow

                That's exactly the effect I was looking for :)

                Sort of a cross between Lawrence of Arabia and Dilbert.[^]

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #10

                Glad to have been of assistance then!

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • S sa alavifar

                  i have a project in c# code and 3 leyer; and all of data base code are in data leyer, which method is better? 1- send sql string statementto data base for select or update? 2 - using stored procedure? in databases with larg scale wich is better? pleas help me?!

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #11
                  1. I haven't written a stored procedure since I started using ADO.net. Before that, with ODBC, I had to write stored procedures in order to have transactions -- that was the only reason we used stored procedures. 1) Stored procedures are not inherently faster than SQL in code.[^] 2) Stored procedures offer some benefits, but I doubt most people capitalize on them (I wouldn't). 3) If you use stored procedures, you still need to have SQL (EXEC statements basically) in code anyway. 4) Most people write stored procedures simply because someone else said they should, not because they know what they're doing. 5) A properly-developed Data Access Layer may remove any need for stored procedures. If there is a need, then stored procedures may be used by the DAL and such details will be hidden from the user. 6) Stored procedures are fragile. 6.1) I have had stored procedures "just disappear" (that was with SQL Server 6, I doubt the problem still exists, but it makes me nervous). 6.2) Stored procedures are too easy to change in the field. All code changes should be in source control, properly built, and deployed to the field in a controlled manner. SQL in code accomplishes that. As mentioned, you absolutely must use parameterized statements. I think one reason people insist on stored procedures is because they force the developer to use parameters. But inline SQL can (and should) use parameters and a well-disciplined developer will have no trouble doing so. I have written several families of classes that make data access with parameters a no-brainer. Here[^]'s one.
                  A 1 Reply Last reply
                  0
                  • S sa alavifar

                    i have a project in c# code and 3 leyer; and all of data base code are in data leyer, which method is better? 1- send sql string statementto data base for select or update? 2 - using stored procedure? in databases with larg scale wich is better? pleas help me?!

                    R Offline
                    R Offline
                    RaviRanjanKr
                    wrote on last edited by
                    #12

                    Always Stored Procedure because it will provide protection from SQL injection attacks, it enable conditional and procedural logic, it provide better management of Compilation and storing or Execution plan it enable of security model and stored procedure code is a lot more readable :laugh: :laugh:

                    modified on Wednesday, October 27, 2010 2:35 AM

                    A 1 Reply Last reply
                    0
                    • R RaviRanjanKr

                      Always Stored Procedure because it will provide protection from SQL injection attacks, it enable conditional and procedural logic, it provide better management of Compilation and storing or Execution plan it enable of security model and stored procedure code is a lot more readable :laugh: :laugh:

                      modified on Wednesday, October 27, 2010 2:35 AM

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #13

                      RaviRanjankr wrote:

                      it will provide protection from SQL injection attacks

                      Not true.

                      RaviRanjankr wrote:

                      it enable conditional and procedural logic, it provide better management of Compilation and storing or Execution plan it enable of security model and Static stored procedure code is a lot more readable

                      True.

                      RaviRanjankr wrote:

                      :laugh: :laugh:

                      Uh... :-O :zzz: :-\ :rolleyes: :thumbsup::thumbsdown:

                      [Forum Guidelines]

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult
                        1. I haven't written a stored procedure since I started using ADO.net. Before that, with ODBC, I had to write stored procedures in order to have transactions -- that was the only reason we used stored procedures. 1) Stored procedures are not inherently faster than SQL in code.[^] 2) Stored procedures offer some benefits, but I doubt most people capitalize on them (I wouldn't). 3) If you use stored procedures, you still need to have SQL (EXEC statements basically) in code anyway. 4) Most people write stored procedures simply because someone else said they should, not because they know what they're doing. 5) A properly-developed Data Access Layer may remove any need for stored procedures. If there is a need, then stored procedures may be used by the DAL and such details will be hidden from the user. 6) Stored procedures are fragile. 6.1) I have had stored procedures "just disappear" (that was with SQL Server 6, I doubt the problem still exists, but it makes me nervous). 6.2) Stored procedures are too easy to change in the field. All code changes should be in source control, properly built, and deployed to the field in a controlled manner. SQL in code accomplishes that. As mentioned, you absolutely must use parameterized statements. I think one reason people insist on stored procedures is because they force the developer to use parameters. But inline SQL can (and should) use parameters and a well-disciplined developer will have no trouble doing so. I have written several families of classes that make data access with parameters a no-brainer. Here[^]'s one.
                        A Offline
                        A Offline
                        AspDotNetDev
                        wrote on last edited by
                        #14

                        Stored procedures can help reduce network traffic, as the EXEC statements are typically shorter than the statements in the SP's. Naturally, this depends on if network traffic is an issue at all (e.g., if the SQL runs on the same machine as the C#, it wouldn't be an issue... it also wouldn't be an issue of the data being transferred dwarfs the SQL code size). DLL's are just as fragile as stored procedures. At my previous job, we had to work with a system that had hundreds of DLL's and OCX's. The systems constantly got out of sync. SP's have one advantage in this respect... you fix them in one location (the database) and they are fixed everywhere. That's kinda nice (at least for systems where there is a primary database server). The other advantage/disadvantage is that you can see the stored procedure code, while it is difficult to see DLL code (without, say, Reflector). That means that you can easily check if the wrong code is in use, but it also means its more tempting for others to modify it on the spot without bothering with version control.

                        PIEBALDconsult wrote:

                        I have had stored procedures "just disappear"

                        SP's should be kept under version control. Sadly, the tools to do this are not excellent. Though, I remember a tool from Redgate under development that is supposed to help with this.

                        PIEBALDconsult wrote:

                        Stored procedures are too easy to change in the field.

                        Shhh, don't tell this to Microsoft... they might think making things "too easy" is a bad idea. ;P Depending on the scenario, this may actually be a good thing. Rather than trying to deploy the code to each computer that uses it, you can simply update the SP's on the server and you're done. This also brings up security concerns. It can be difficult or impossible to control security if you allow the clients to run arbitrary SQL statements. If you have stored procedures, you can control exactly what executes on the server. This is a very important point, as it may be the single greatest advantage stored procedures have over plain SQL code.

                        PIEBALDconsult wrote:

                        one reason people insist on stored procedures is because they force the developer to use parameters

                        string firstName = "'; DROP TABLE Whatever; --";
                        string cmd = "EXEC SelectData '" + firstName + "'";

                        Both stored procedures and plain SQL are susceptible to SQL injection.

                        P 1 Reply Last reply
                        0
                        • A AspDotNetDev

                          Stored procedures can help reduce network traffic, as the EXEC statements are typically shorter than the statements in the SP's. Naturally, this depends on if network traffic is an issue at all (e.g., if the SQL runs on the same machine as the C#, it wouldn't be an issue... it also wouldn't be an issue of the data being transferred dwarfs the SQL code size). DLL's are just as fragile as stored procedures. At my previous job, we had to work with a system that had hundreds of DLL's and OCX's. The systems constantly got out of sync. SP's have one advantage in this respect... you fix them in one location (the database) and they are fixed everywhere. That's kinda nice (at least for systems where there is a primary database server). The other advantage/disadvantage is that you can see the stored procedure code, while it is difficult to see DLL code (without, say, Reflector). That means that you can easily check if the wrong code is in use, but it also means its more tempting for others to modify it on the spot without bothering with version control.

                          PIEBALDconsult wrote:

                          I have had stored procedures "just disappear"

                          SP's should be kept under version control. Sadly, the tools to do this are not excellent. Though, I remember a tool from Redgate under development that is supposed to help with this.

                          PIEBALDconsult wrote:

                          Stored procedures are too easy to change in the field.

                          Shhh, don't tell this to Microsoft... they might think making things "too easy" is a bad idea. ;P Depending on the scenario, this may actually be a good thing. Rather than trying to deploy the code to each computer that uses it, you can simply update the SP's on the server and you're done. This also brings up security concerns. It can be difficult or impossible to control security if you allow the clients to run arbitrary SQL statements. If you have stored procedures, you can control exactly what executes on the server. This is a very important point, as it may be the single greatest advantage stored procedures have over plain SQL code.

                          PIEBALDconsult wrote:

                          one reason people insist on stored procedures is because they force the developer to use parameters

                          string firstName = "'; DROP TABLE Whatever; --";
                          string cmd = "EXEC SelectData '" + firstName + "'";

                          Both stored procedures and plain SQL are susceptible to SQL injection.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #15

                          aspdotnetdev wrote:

                          Stored procedures can help reduce network traffic

                          Yes, but mine never go across a network so that argument has no weight for me.

                          aspdotnetdev wrote:

                          you fix them in one location (the database) and they are fixed everywhere

                          Not if you have many clients each with his own database. And you should be putting the code in a version control system, that makes at least two places.

                          aspdotnetdev wrote:

                          It can be difficult or impossible to control security if you allow the clients to run arbitrary SQL statements.

                          That's what a DAL is for.

                          aspdotnetdev wrote:

                          If you have stored procedures, you can control exactly what executes on the server.

                          You can. But do you? It's much easier with compiled code.

                          aspdotnetdev wrote:

                          Both stored procedures and plain SQL are susceptible to SQL injection.

                          Huh, OK.

                          1 Reply Last reply
                          0
                          • S sa alavifar

                            i have a project in c# code and 3 leyer; and all of data base code are in data leyer, which method is better? 1- send sql string statementto data base for select or update? 2 - using stored procedure? in databases with larg scale wich is better? pleas help me?!

                            B Offline
                            B Offline
                            brunoseixas
                            wrote on last edited by
                            #16

                            use store procedures in database because if you have any problem, you only needs change the proc to resolve

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups