Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Which one is a better [InsertCommand]...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RP - 12 Oct 2007 09:03 GMT
>From the below given codes used to insert a record in a table, which
code is well-optimized and must be used. Please also let me know why
it is better.

=======[ CODE 1 ]===========================================
       public Int32 InsertNewRecord(string myQuery)
       {
           objModCon.OpenConnection();
           SqlCommand cmdInsert = new SqlCommand(myQuery,
objModCon.myCN);
           try
           {
               Int32 RecordsAffected = cmdInsert.ExecuteNonQuery();
               return RecordsAffected;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Routine: ModReUsable-
InsertNewRecord(" + myQuery + ") " + ex.ToString(), "Error:",
MessageBoxButtons.OK, MessageBoxIcon.Error);
               return 0;
           }
           finally
           {
               cmdInsert.Dispose();
               objModCon.CloseConnection();
           }
       }
========[ CODE 2 ]==================================================
public static OleDbDataAdapter CreateCustomerAdapter(
   OleDbConnection connection)
{
   OleDbDataAdapter adapter = new OleDbDataAdapter();
   OleDbCommand command;

   // Create the SelectCommand.
   command = new OleDbCommand("SELECT CustomerID FROM Customers " +
       "WHERE Country = ? AND City = ?", connection);

   command.Parameters.Add("Country", OleDbType.VarChar, 15);
   command.Parameters.Add("City", OleDbType.VarChar, 15);

   adapter.SelectCommand = command;

   // Create the InsertCommand.
   command = new OleDbCommand(
       "INSERT INTO Customers (CustomerID, CompanyName) " +
       "VALUES (?, ?)", connection);

   command.Parameters.Add(
       "CustomerID", OleDbType.Char, 5, "CustomerID");
   command.Parameters.Add(
       "CompanyName", OleDbType.VarChar, 40, "CompanyName");

   adapter.InsertCommand = command;
   return adapter;
}
=====================================================================================================
Göran Andersson - 12 Oct 2007 09:57 GMT
>>From the below given codes used to insert a record in a table, which
> code is well-optimized and must be used. Please also let me know why
> it is better.

Neither.

For only the purpose of inserting a record, just take out the code that
creates the insert command from the second piece of code, and skip the
data adapter entirely. Use the ExecuteNonQuery method to run it, just
like in the first piece of code.

Reasons:

There is no need for a data adapter to insert a record. You only need
the command.

You should always use parameters with your command:

- Putting the values directly in the query is difficult without exposing
your code to the risk of SQL injection.

- The database can cache the execution plan for parameterised queries,
as the query doesn't change when the values change. (Not really
important for just an insert, but relevant for more complex queries.)

Signature

Göran Andersson
_____
http://www.guffa.com

Cor Ligthert[MVP] - 16 Oct 2007 05:33 GMT
RP,

This question sounds for me a little bit the same as asking "what is better
a plane or a car".

Beside that both codes are not quiet as good as it can be for several
reasons. Are you using C# 2003 by the way?

Cor

> >From the below given codes used to insert a record in a table, which
> code is well-optimized and must be used. Please also let me know why
[quoted text clipped - 54 lines]
> }
> =====================================================================================================

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.