Showing posts with label re-use. Show all posts
Showing posts with label re-use. Show all posts

Friday, March 9, 2012

Re-use SqlCommand object

Is it ok to re-use a SqlCommand object? In a method, I am executing 2 separate parameterized sql statements. Before I run the second, I will clear the command objects parameters.(command.parameters.clear()) I'm just checking to see if it is good coding practice or not.

thanks,

SC

Half the reason command objects exist actualy. Go right ahead!|||

I think it's OK to re-use a SqlCommand object, if you can make sure to finish using all objects associated with the SqlCommand before reusing it. However I won't suggest to reuse a SqlCommand for multiple purpose, it's better to keep each SqlCommand for single task and Dispose the objects after using it.

re-use rowguid, replication fails?

My ROWGUID columns are also my primary keys in many cases. I have
some code that deletes and then reinserts a row that has changes
instead of doing an UPDATE. As such, I re-use the same guid in the
ROWGUID column.
I have found that when I do this, the changes to the row do not make
it back up to the master server during a synchronize, as if the
replication does not recognize that the delete and re-insert has been
done on the row. If I do an update on the row, the change is
recognized and sent back to the master server.
Is this an expected behavior? Is it a no-no to delete a row with
ROWGUID x, then re-insert a new row with the same ROWGUID x?
I can probably whip up a step-by-step example that demonstrates this
behavior if desired.
thanks,
matthew tagliaferri
I take it you are using merge replication. Merge replication uses the
rowguid column to track which row has changed. Its probably not a good idea
to modify this row.
Can you check your conflict tables using the conflict viewer to see if any
conflicts are being logged. If not, please post your repo.
Hilary Cotter
Looking for a SQL Server replication book?
Now available for purchase at:
http://www.nwsu.com/0974973602.html
"matt tagliaferri" <mtagliaf@.cleindians.com> wrote in message
news:2cc74fa4.0411181102.5406ca87@.posting.google.c om...
> My ROWGUID columns are also my primary keys in many cases. I have
> some code that deletes and then reinserts a row that has changes
> instead of doing an UPDATE. As such, I re-use the same guid in the
> ROWGUID column.
> I have found that when I do this, the changes to the row do not make
> it back up to the master server during a synchronize, as if the
> replication does not recognize that the delete and re-insert has been
> done on the row. If I do an update on the row, the change is
> recognized and sent back to the master server.
> Is this an expected behavior? Is it a no-no to delete a row with
> ROWGUID x, then re-insert a new row with the same ROWGUID x?
> I can probably whip up a step-by-step example that demonstrates this
> behavior if desired.
> thanks,
> matthew tagliaferri
|||No conflicts exist. I can look at the table in question on the "master"
and "child" server, and the data in the child server is newer than the
data in the master server.
I spent a bit of time setting up some replication logging, this was
useful only in the fact that it didn't show any replication activity on
the table in question.
My collegue and I are setting up a reproducible example now, I will post
here when it is complete.
thanks for the reply,
matt tagliaferri
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!

Re-use of SqlConnection and SqlCommand ?

Hi,

When using the following controls...

System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlCommand

If I want to change my SQL command and execute the query once again what cleanup do I need to do first?

Do I need close and dispose the SqlConnection?Do I need to dispose the SqlCommand?

Can I use the SqlConnection for more than one SqlCommand?

Thanks,
Scott

you can use sqlconnection over and over. the rule on dispose, is if the class implements idisposable, you should call dispose. I believe both of these do, so you should. I would suggest us the "using" syntax and then idispose will be called for you and you don't have to worry about it.

|||Thanks.

Forgive my ignorance, what is the 'using' syntax?
I would google it, but 'using' is too generic of a word to get anything useful.

Here is some sample code of mine...

System.Data.SqlClient.SqlConnection con_dupProjectId =new System.Data.SqlClient.SqlConnection(); con_dupProjectId.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RIIDBudget"].ToString(); con_dupProjectId.Open(); System.Data.SqlClient.SqlCommand cmd_dupProjectId =new System.Data.SqlClient.SqlCommand(); cmd_dupProjectId.Connection = con_dupProjectId; cmd_dupProjectId.CommandText ="select * from tblEORSummary where project_id in " +"(select project_id from tblEORSummary group by project_id having count(project_id) >1) " +"order by project_id"; System.Data.SqlClient.SqlDataReader rdr_dupProjectId = cmd_dupProjectId.ExecuteReader();
|||

Sorry, not a C# expert, but it's something like this:

using (System.Data.SqlClient.SqlConnection con_dupProjectId =new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RIIDBudget"].ToString()))

{
con_dupProjectId.Open();
using (System.Data.SqlClient.SqlCommand cmd_dupProjectId =new System.Data.SqlClient.SqlCommand())

{
cmd_dupProjectId.Connection = con_dupProjectId;
cmd_dupProjectId.CommandText ="select * from tblEORSummary where project_id in " +
"(select project_id from tblEORSummary group by project_id having count(project_id) >1) " +
"order by project_id";
System.Data.SqlClient.SqlDataReader rdr_dupProjectId = cmd_dupProjectId.ExecuteReader();

}

}

|||

Thanks, back to the original question...

I tried the following...

System.Data.SqlClient.SqlConnection con_RIID =new System.Data.SqlClient.SqlConnection();
con_RIID.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RIIDBudget"].ToString();
con_RIID.Open();

System.Data.SqlClient.SqlCommand cmd_dupProjectId =new System.Data.SqlClient.SqlCommand();
cmd_dupProjectId.Connection = con_RIID;
cmd_dupProjectId.CommandText ="select * from tblEORSummary1025 where project_id in " +
"(select project_id from tblEORSummary1025 group by project_id having count(project_id) >1) " +
"order by project_id";
System.Data.SqlClient.SqlDataReader rdr_dupProjectId = cmd_dupProjectId.ExecuteReader();
int prev_project_id = -1;
int project_id = 0;
while (rdr_dupProjectId.Read())
{
project_id = (int)rdr_dupProjectId["project_id"];
if (prev_project_id == project_id)// this way we skip the first record
{
string current_fiscal_year = (string)rdr_dupProjectId["current_fiscal_year"];
int external_system_id = (int)rdr_dupProjectId["external_system_id"];
int eor_summary_id = (int)rdr_dupProjectId["eor_summary_id"];

// 2) Creat new record in project_main with the new project_id
System.Data.SqlClient.SqlCommand cmd_insertProjectMain =new System.Data.SqlClient.SqlCommand();
cmd_insertProjectMain.Connection = con_RIID;
cmd_insertProjectMain.CommandText ="insert project_main1025 (long_title, short_title, pi_name, task_area, riid_plan_number, plan_number, principal_investigator, co_principal_investigator, " +
"pi_institute, pi_division, new_start, program_element, apc_id, pi_office, pi_phone, pi_lab_room, bsl_level_required, active_animal_protocol, " +
"fiscal_year, project_type, project_source, division, external_system_id, person_id, editable) " +
"select long_title, short_title, pi_name, task_area, riid_plan_number, plan_number, principal_investigator, co_principal_investigator, " +
"pi_institute, pi_division, 0, program_element, apc_id, pi_office, pi_phone, pi_lab_room, bsl_level_required, active_animal_protocol, " +
"'" + current_fiscal_year +"', " +
"project_type, project_source, division, external_system_id, person_id, editable " +
"from project_main1025 " +
"where project_id =" + project_id;
cmd_insertProjectMain.ExecuteNonQuery();
...

First time inside the 'if' clause I get an error...

"There is already an open DataReader associated with this Command which must be closed first."

The error occurs on the last line of the code I have posted.


|||

You can not have a datareader open on a connection, and then try to use a command that uses that same connection (It's tied up with the datareader).

Either read everything you want from the datareader, then close the datareader, then iterate through your data

or

Use a dataset instead of a datareader and iterate through that

or

Declare, initialize, and open a 2nd connection object, and associate the 2nd command with that connection instead.

Re-use filter

I have a chunk of SQL that is use if a particular report needs to have
a point in time parameter. Is there any way to store this code so any
report in the solution has access to it? I don't want any business
users that develop reports to have to type all the SQL in every time.
Any thoughts?You could create a custom assembly that returned this chunk of SQL and then
construct your commands through an expression that calls your custom
assembly.
--
Brian Welcker
Group Program Manager
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Austin S" <austinswope@.gmail.com> wrote in message
news:b68c96f7.0409281425.27324a81@.posting.google.com...
>I have a chunk of SQL that is use if a particular report needs to have
> a point in time parameter. Is there any way to store this code so any
> report in the solution has access to it? I don't want any business
> users that develop reports to have to type all the SQL in every time.
> Any thoughts?

Re-use data from SqlDataSource

I have a page containing a FormView, which gets its data from a SqlDataSource control and displays details of a job. Two of the fields are location and job title. I want to re-use this data to create a dynamic page title. I know I can do this by setting the Page.Title to what I want to, but do I have access to the data outside of the FormView to which the data source is bound? If so how? Or will I have to perform an additional SELECT statement to get this data again?

Hi

No need to perform additional Select statement. You can access the FormView data in DataBound event. Try the following code:

protected void FormView1_DataBound(object sender, EventArgs e) { Page.Title = ((Label)(FormView1.Row.FindControl("column1Label"))).Text; }
Hope this helps.

|||

Brilliant. I've got that working nicely now, and discovered more events to play withBig Smile

Thank you.