Inserting data in database in Asp.Net . Which way is better? [closed]










0














I am working on a Asp.Net website to insert data in the database. After reading different articles on internet where it is suggested that we should use a parametrized query as it prevents from SQL Injection attacks.



So I am wondering which way is better:



Create a Stored Procedure with parameters in the database and then call it in the button click event to insert the data in the database e.g.



CREATE PROCEDURE AddInfraction
@Description varchar(255), @Penalty money, @Points int
AS
BEGIN
INSERT INTO Infractions (Description, Penalty, Points)
VALUES (@Description, @Penalty, @Points)
END

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))

using (SqlCommand command = new SqlCommand("AddInfraction"))

command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("Description", Description.Text);
command.Parameters.AddWithValue("Penalty", Convert.ToInt16(Penalty.Text));
command.Parameters.AddWithValue("Points", Convert.ToInt16(Points.Text));
connection.Open();
queryResult = command.ExecuteNonQuery();
if (queryResult == 0)

return;


connection.Close();



Or maybe first by adding the ADO.NET Entity Data Model in the database and then in the button click event creating the object of the data model recently added, and then call the particular stored procedure and inserting the data in the database.



using (ETrafficChallanSystemEntities eTrafficChallanSystemEntities = new ETrafficChallanSystemEntities())

eTrafficChallanSystemEntities.AddInfraction(Description.Text,
Convert.ToInt16(Penalty.Text), Convert.ToInt16(Points.Text));



Which one would be the best way to insert data in the database.










share|improve this question















closed as primarily opinion-based by Daniel A. White, S.Akbari, Manfred Radlwimmer, AdrianHHH, EdChum Nov 10 '18 at 16:54


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.














  • The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
    – Crowcoder
    Nov 10 '18 at 14:00










  • Is it safe enough from SQL Injection Attacks
    – SameerAli
    Nov 10 '18 at 14:02






  • 2




    yep. its safe since you are using parameters.
    – Daniel A. White
    Nov 10 '18 at 14:03










  • Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
    – Christopher
    Nov 10 '18 at 14:22















0














I am working on a Asp.Net website to insert data in the database. After reading different articles on internet where it is suggested that we should use a parametrized query as it prevents from SQL Injection attacks.



So I am wondering which way is better:



Create a Stored Procedure with parameters in the database and then call it in the button click event to insert the data in the database e.g.



CREATE PROCEDURE AddInfraction
@Description varchar(255), @Penalty money, @Points int
AS
BEGIN
INSERT INTO Infractions (Description, Penalty, Points)
VALUES (@Description, @Penalty, @Points)
END

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))

using (SqlCommand command = new SqlCommand("AddInfraction"))

command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("Description", Description.Text);
command.Parameters.AddWithValue("Penalty", Convert.ToInt16(Penalty.Text));
command.Parameters.AddWithValue("Points", Convert.ToInt16(Points.Text));
connection.Open();
queryResult = command.ExecuteNonQuery();
if (queryResult == 0)

return;


connection.Close();



Or maybe first by adding the ADO.NET Entity Data Model in the database and then in the button click event creating the object of the data model recently added, and then call the particular stored procedure and inserting the data in the database.



using (ETrafficChallanSystemEntities eTrafficChallanSystemEntities = new ETrafficChallanSystemEntities())

eTrafficChallanSystemEntities.AddInfraction(Description.Text,
Convert.ToInt16(Penalty.Text), Convert.ToInt16(Points.Text));



Which one would be the best way to insert data in the database.










share|improve this question















closed as primarily opinion-based by Daniel A. White, S.Akbari, Manfred Radlwimmer, AdrianHHH, EdChum Nov 10 '18 at 16:54


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.














  • The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
    – Crowcoder
    Nov 10 '18 at 14:00










  • Is it safe enough from SQL Injection Attacks
    – SameerAli
    Nov 10 '18 at 14:02






  • 2




    yep. its safe since you are using parameters.
    – Daniel A. White
    Nov 10 '18 at 14:03










  • Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
    – Christopher
    Nov 10 '18 at 14:22













0












0








0







I am working on a Asp.Net website to insert data in the database. After reading different articles on internet where it is suggested that we should use a parametrized query as it prevents from SQL Injection attacks.



So I am wondering which way is better:



Create a Stored Procedure with parameters in the database and then call it in the button click event to insert the data in the database e.g.



CREATE PROCEDURE AddInfraction
@Description varchar(255), @Penalty money, @Points int
AS
BEGIN
INSERT INTO Infractions (Description, Penalty, Points)
VALUES (@Description, @Penalty, @Points)
END

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))

using (SqlCommand command = new SqlCommand("AddInfraction"))

command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("Description", Description.Text);
command.Parameters.AddWithValue("Penalty", Convert.ToInt16(Penalty.Text));
command.Parameters.AddWithValue("Points", Convert.ToInt16(Points.Text));
connection.Open();
queryResult = command.ExecuteNonQuery();
if (queryResult == 0)

return;


connection.Close();



Or maybe first by adding the ADO.NET Entity Data Model in the database and then in the button click event creating the object of the data model recently added, and then call the particular stored procedure and inserting the data in the database.



using (ETrafficChallanSystemEntities eTrafficChallanSystemEntities = new ETrafficChallanSystemEntities())

eTrafficChallanSystemEntities.AddInfraction(Description.Text,
Convert.ToInt16(Penalty.Text), Convert.ToInt16(Points.Text));



Which one would be the best way to insert data in the database.










share|improve this question















I am working on a Asp.Net website to insert data in the database. After reading different articles on internet where it is suggested that we should use a parametrized query as it prevents from SQL Injection attacks.



So I am wondering which way is better:



Create a Stored Procedure with parameters in the database and then call it in the button click event to insert the data in the database e.g.



CREATE PROCEDURE AddInfraction
@Description varchar(255), @Penalty money, @Points int
AS
BEGIN
INSERT INTO Infractions (Description, Penalty, Points)
VALUES (@Description, @Penalty, @Points)
END

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))

using (SqlCommand command = new SqlCommand("AddInfraction"))

command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("Description", Description.Text);
command.Parameters.AddWithValue("Penalty", Convert.ToInt16(Penalty.Text));
command.Parameters.AddWithValue("Points", Convert.ToInt16(Points.Text));
connection.Open();
queryResult = command.ExecuteNonQuery();
if (queryResult == 0)

return;


connection.Close();



Or maybe first by adding the ADO.NET Entity Data Model in the database and then in the button click event creating the object of the data model recently added, and then call the particular stored procedure and inserting the data in the database.



using (ETrafficChallanSystemEntities eTrafficChallanSystemEntities = new ETrafficChallanSystemEntities())

eTrafficChallanSystemEntities.AddInfraction(Description.Text,
Convert.ToInt16(Penalty.Text), Convert.ToInt16(Points.Text));



Which one would be the best way to insert data in the database.







c# sql asp.net stored-procedures ado.net-entity-data-model






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 14:04

























asked Nov 10 '18 at 13:52









SameerAli

93




93




closed as primarily opinion-based by Daniel A. White, S.Akbari, Manfred Radlwimmer, AdrianHHH, EdChum Nov 10 '18 at 16:54


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as primarily opinion-based by Daniel A. White, S.Akbari, Manfred Radlwimmer, AdrianHHH, EdChum Nov 10 '18 at 16:54


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.













  • The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
    – Crowcoder
    Nov 10 '18 at 14:00










  • Is it safe enough from SQL Injection Attacks
    – SameerAli
    Nov 10 '18 at 14:02






  • 2




    yep. its safe since you are using parameters.
    – Daniel A. White
    Nov 10 '18 at 14:03










  • Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
    – Christopher
    Nov 10 '18 at 14:22
















  • The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
    – Crowcoder
    Nov 10 '18 at 14:00










  • Is it safe enough from SQL Injection Attacks
    – SameerAli
    Nov 10 '18 at 14:02






  • 2




    yep. its safe since you are using parameters.
    – Daniel A. White
    Nov 10 '18 at 14:03










  • Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
    – Christopher
    Nov 10 '18 at 14:22















The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
– Crowcoder
Nov 10 '18 at 14:00




The best way is your preference. Personally, I would not use a stored procedure if I was using Entity Framework.
– Crowcoder
Nov 10 '18 at 14:00












Is it safe enough from SQL Injection Attacks
– SameerAli
Nov 10 '18 at 14:02




Is it safe enough from SQL Injection Attacks
– SameerAli
Nov 10 '18 at 14:02




2




2




yep. its safe since you are using parameters.
– Daniel A. White
Nov 10 '18 at 14:03




yep. its safe since you are using parameters.
– Daniel A. White
Nov 10 '18 at 14:03












Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
– Christopher
Nov 10 '18 at 14:22




Parametized queries are both the safest and potentially faster (because a lot of checks/translation can be done on the Programm side). No idea where ADO.Net comapres to it. But if you need explicit casting, that is usually a bad sign for type safety and thus Injection Proofing.
– Christopher
Nov 10 '18 at 14:22












4 Answers
4






active

oldest

votes


















0














Both the methods are good to insert data in the database. Using a parameterized query is always a good option. I would suggest using the second method as you have already added the ADO.NET Entity Model in your project.






share|improve this answer




























    1














    as mentioned, this should be your preference. however with the recent trend of microservices patterns, it's likely better to tie less of your application logic to a database product. really depends on what the application is for and how often the backend will shift






    share|improve this answer




























      0














      There is not an absolute answer to this question. Any program(Application) has own requirement, but I think about some points on using each method.



      A) Using Stored Procedures Pros:



      • For the first time run is slow but for next times is so fast.

      • Data Processing logic is unique and integrate, So changing that is
        so easy, understandable, fast, liable and clean.

      • Migrating from one IDE/Language/Maybe Framework to another is easy because most business logics are in DB layer, So many of cod changing converting stored procedure call in the new language.

      B) Using the Entity Data Model Pros:



      • Didn't spend the time to write safe and powerful Stored procedures.


      • Data Processing logic can be changed slowly and through the coding
        progress.


      • Having meaningful and clear sight of DB entities(Tables/Views and
        relations) In coding time.


      • can be changed DataBase architecture in some scenarios such as
        Code-First programming.


      After all, I think Maybe better to keep Entity Model way for the most changeable/front-end related/little process section and Using Stored Procedures for persistent/Deep Backend related/Huge and multi-process sections.






      share|improve this answer




























        0














        I don't recommend using Stored Procedure for simple read/write queries, for the following reasons:



        • Some logic is implemented in the database and is outside your source control

        • Harder to maintain

        • More verbosity for the project

        For your case Entity Framework (Microsoft Database Framework) can cover almost 90% of cases.



        Of if you want more control over your Sql Queries, you can use a Query Builder like Sql Kata (I am the author of this library)






        share|improve this answer



























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Both the methods are good to insert data in the database. Using a parameterized query is always a good option. I would suggest using the second method as you have already added the ADO.NET Entity Model in your project.






          share|improve this answer

























            0














            Both the methods are good to insert data in the database. Using a parameterized query is always a good option. I would suggest using the second method as you have already added the ADO.NET Entity Model in your project.






            share|improve this answer























              0












              0








              0






              Both the methods are good to insert data in the database. Using a parameterized query is always a good option. I would suggest using the second method as you have already added the ADO.NET Entity Model in your project.






              share|improve this answer












              Both the methods are good to insert data in the database. Using a parameterized query is always a good option. I would suggest using the second method as you have already added the ADO.NET Entity Model in your project.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 '18 at 14:14









              Junaid Sultan

              1251111




              1251111























                  1














                  as mentioned, this should be your preference. however with the recent trend of microservices patterns, it's likely better to tie less of your application logic to a database product. really depends on what the application is for and how often the backend will shift






                  share|improve this answer

























                    1














                    as mentioned, this should be your preference. however with the recent trend of microservices patterns, it's likely better to tie less of your application logic to a database product. really depends on what the application is for and how often the backend will shift






                    share|improve this answer























                      1












                      1








                      1






                      as mentioned, this should be your preference. however with the recent trend of microservices patterns, it's likely better to tie less of your application logic to a database product. really depends on what the application is for and how often the backend will shift






                      share|improve this answer












                      as mentioned, this should be your preference. however with the recent trend of microservices patterns, it's likely better to tie less of your application logic to a database product. really depends on what the application is for and how often the backend will shift







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 '18 at 14:46









                      Ming L

                      111




                      111





















                          0














                          There is not an absolute answer to this question. Any program(Application) has own requirement, but I think about some points on using each method.



                          A) Using Stored Procedures Pros:



                          • For the first time run is slow but for next times is so fast.

                          • Data Processing logic is unique and integrate, So changing that is
                            so easy, understandable, fast, liable and clean.

                          • Migrating from one IDE/Language/Maybe Framework to another is easy because most business logics are in DB layer, So many of cod changing converting stored procedure call in the new language.

                          B) Using the Entity Data Model Pros:



                          • Didn't spend the time to write safe and powerful Stored procedures.


                          • Data Processing logic can be changed slowly and through the coding
                            progress.


                          • Having meaningful and clear sight of DB entities(Tables/Views and
                            relations) In coding time.


                          • can be changed DataBase architecture in some scenarios such as
                            Code-First programming.


                          After all, I think Maybe better to keep Entity Model way for the most changeable/front-end related/little process section and Using Stored Procedures for persistent/Deep Backend related/Huge and multi-process sections.






                          share|improve this answer

























                            0














                            There is not an absolute answer to this question. Any program(Application) has own requirement, but I think about some points on using each method.



                            A) Using Stored Procedures Pros:



                            • For the first time run is slow but for next times is so fast.

                            • Data Processing logic is unique and integrate, So changing that is
                              so easy, understandable, fast, liable and clean.

                            • Migrating from one IDE/Language/Maybe Framework to another is easy because most business logics are in DB layer, So many of cod changing converting stored procedure call in the new language.

                            B) Using the Entity Data Model Pros:



                            • Didn't spend the time to write safe and powerful Stored procedures.


                            • Data Processing logic can be changed slowly and through the coding
                              progress.


                            • Having meaningful and clear sight of DB entities(Tables/Views and
                              relations) In coding time.


                            • can be changed DataBase architecture in some scenarios such as
                              Code-First programming.


                            After all, I think Maybe better to keep Entity Model way for the most changeable/front-end related/little process section and Using Stored Procedures for persistent/Deep Backend related/Huge and multi-process sections.






                            share|improve this answer























                              0












                              0








                              0






                              There is not an absolute answer to this question. Any program(Application) has own requirement, but I think about some points on using each method.



                              A) Using Stored Procedures Pros:



                              • For the first time run is slow but for next times is so fast.

                              • Data Processing logic is unique and integrate, So changing that is
                                so easy, understandable, fast, liable and clean.

                              • Migrating from one IDE/Language/Maybe Framework to another is easy because most business logics are in DB layer, So many of cod changing converting stored procedure call in the new language.

                              B) Using the Entity Data Model Pros:



                              • Didn't spend the time to write safe and powerful Stored procedures.


                              • Data Processing logic can be changed slowly and through the coding
                                progress.


                              • Having meaningful and clear sight of DB entities(Tables/Views and
                                relations) In coding time.


                              • can be changed DataBase architecture in some scenarios such as
                                Code-First programming.


                              After all, I think Maybe better to keep Entity Model way for the most changeable/front-end related/little process section and Using Stored Procedures for persistent/Deep Backend related/Huge and multi-process sections.






                              share|improve this answer












                              There is not an absolute answer to this question. Any program(Application) has own requirement, but I think about some points on using each method.



                              A) Using Stored Procedures Pros:



                              • For the first time run is slow but for next times is so fast.

                              • Data Processing logic is unique and integrate, So changing that is
                                so easy, understandable, fast, liable and clean.

                              • Migrating from one IDE/Language/Maybe Framework to another is easy because most business logics are in DB layer, So many of cod changing converting stored procedure call in the new language.

                              B) Using the Entity Data Model Pros:



                              • Didn't spend the time to write safe and powerful Stored procedures.


                              • Data Processing logic can be changed slowly and through the coding
                                progress.


                              • Having meaningful and clear sight of DB entities(Tables/Views and
                                relations) In coding time.


                              • can be changed DataBase architecture in some scenarios such as
                                Code-First programming.


                              After all, I think Maybe better to keep Entity Model way for the most changeable/front-end related/little process section and Using Stored Procedures for persistent/Deep Backend related/Huge and multi-process sections.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 10 '18 at 15:39









                              QMaster

                              2,0942244




                              2,0942244





















                                  0














                                  I don't recommend using Stored Procedure for simple read/write queries, for the following reasons:



                                  • Some logic is implemented in the database and is outside your source control

                                  • Harder to maintain

                                  • More verbosity for the project

                                  For your case Entity Framework (Microsoft Database Framework) can cover almost 90% of cases.



                                  Of if you want more control over your Sql Queries, you can use a Query Builder like Sql Kata (I am the author of this library)






                                  share|improve this answer

























                                    0














                                    I don't recommend using Stored Procedure for simple read/write queries, for the following reasons:



                                    • Some logic is implemented in the database and is outside your source control

                                    • Harder to maintain

                                    • More verbosity for the project

                                    For your case Entity Framework (Microsoft Database Framework) can cover almost 90% of cases.



                                    Of if you want more control over your Sql Queries, you can use a Query Builder like Sql Kata (I am the author of this library)






                                    share|improve this answer























                                      0












                                      0








                                      0






                                      I don't recommend using Stored Procedure for simple read/write queries, for the following reasons:



                                      • Some logic is implemented in the database and is outside your source control

                                      • Harder to maintain

                                      • More verbosity for the project

                                      For your case Entity Framework (Microsoft Database Framework) can cover almost 90% of cases.



                                      Of if you want more control over your Sql Queries, you can use a Query Builder like Sql Kata (I am the author of this library)






                                      share|improve this answer












                                      I don't recommend using Stored Procedure for simple read/write queries, for the following reasons:



                                      • Some logic is implemented in the database and is outside your source control

                                      • Harder to maintain

                                      • More verbosity for the project

                                      For your case Entity Framework (Microsoft Database Framework) can cover almost 90% of cases.



                                      Of if you want more control over your Sql Queries, you can use a Query Builder like Sql Kata (I am the author of this library)







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 10 '18 at 15:48









                                      amd

                                      13.7k33550




                                      13.7k33550













                                          Popular posts from this blog

                                          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                          Crossroads (UK TV series)

                                          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế