[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Mandatory to call GetUpdateCommand of SqlCommandBuilder ?

Posted on 2009-07-13
4
Medium Priority
?
1,015 Views
Last Modified: 2012-05-07
I am updating/inserting database via dataset.
According to the sample code from Microsoft documentation, it is required to call GetUpdateCommand() and GetInsertCommand() of the command builder before doing
adapter.Update(ds, dataTableName).

But my program works fine even without calling these.
Could somebody tell if I really need to call these methods ?
Without that, what kind of problem could happen?
SqlConnection conn = new SqlConnection(CONNECTION_STRING);
 
            string dataTableName = "PRICE_DATA";
 
            DataSet ds = new DataSet(dataTableName);
 
            string sql = "SELECT Ticker, As_of, Updt_dt, eqy_weighted_avg_px, px_open, px_volume, asset_id, modifiedtimestamp FROM Price WHERE Updt_dt = '" + update_Date + "'";
 
            SqlCommand selectCMD = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(selectCMD);
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
 
            conn.Open();
 
            try
            {
 
                da.Fill(ds, dataTableName);
 
            }
            catch (SqlException ex)
            {
 
                throw ex;
            }
 
 
            //Setting composite PK
            DataColumn[] keys = new DataColumn[2];
            DataColumn column1 = ds.Tables[0].Columns["TICKER"];
            DataColumn column2 = ds.Tables[0].Columns["UPDT_DT"];
 
            keys[0] = column1;
            keys[1] = column2;
 
            ds.Tables[0].PrimaryKey = keys;
 
 
            
            foreach (Security sec in securities)
            {
 
                if (sec.OpenPrice != 0 && sec.Volume != 0  && sec.WeightedAverage != 0) //if value is not available, do not insert
                {
                    //Key Values
                    object[] myKeyValues = { sec.Ticker, sec.UpdateDate.ToString("yyyyMMdd") };
 
                    if (ds.Tables[dataTableName].Rows.Contains(myKeyValues))
                    {
                        //UPDATE
 
                        DataRow existingRow = ds.Tables[dataTableName].Rows.Find(myKeyValues);
 
                        existingRow["AS_OF"] = sec.AsOfDate.ToString("yyyyMMdd");
                        existingRow["PX_OPEN"] = sec.OpenPrice;
                        existingRow["PX_VOLUME"] = sec.Volume;
                        existingRow["EQY_WEIGHTED_AVG_PX"] = sec.WeightedAverage;
                        existingRow["MODIFIEDTIMESTAMP"] = DateTime.Now;
 
                    }
                    else
                    {
                        //INSERT
 
                        DataRow newRow = ds.Tables[dataTableName].NewRow();
 
                        newRow["TICKER"] = sec.Ticker;
                        newRow["AS_OF"] = sec.AsOfDate.ToString("yyyyMMdd");
                        newRow["UPDT_DT"] = DateTime.Now.Date.ToString("yyyyMMdd");
                        newRow["EQY_WEIGHTED_AVG_PX"] = sec.WeightedAverage;
                        newRow["PX_OPEN"] = sec.OpenPrice;
                        newRow["PX_VOLUME"] = sec.Volume;
                        newRow["ASSET_ID"] = sec.AssetId;
                        newRow["MODIFIEDTIMESTAMP"] = DateTime.Now;
 
                        ds.Tables[0].Rows.Add(newRow);
 
 
                    }
 
                }
 
 
            }
 
 
            builder.GetUpdateCommand(); //not necessary ?
            builder.GetInsertCommand();//not necessary ?
 
                
	    da.Update(ds, dataTableName);

Open in new window

0
Comment
Question by:Takamasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24850354
If you reach da.Update(ds, dataTableName) without calling GetUpdateCommand and GetInsertCommand, does the SqlDataAdapter have an instance of an UpdateCommand and InsertCommand?  I don't see anything in the SqlCommandBuilder or DbCommandBuilder (base class), that would construct the commands otherwise.
0
 

Author Comment

by:Takamasa
ID: 24856811
Hi. Thank you for the response.
No, both UpdateCommand and InsertCommand are null...
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 24858439
Then, I would suggest that there isn't any updates or inserts, or there should have been an exception.  I always use the GetUpdateCommand, GetInsertCommand, and GetDeleteCommand with command builders.
0
 

Author Comment

by:Takamasa
ID: 24865560
Hello TheLearnedOne,
Thank you for your comment again.
Strangely the data do get inserted in my database, which made me wonder what is the use of GetInsertCommand. Anyways, I will use the GetUpdateCommand and GetInsertCommand like you suggested.
Thank you!
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question