Am i calling the database twice?

K

K Viltersten

I run the following code, which works well and gives
me the expected results.

string cmdStr = "select Stuff from There";
SqlCommand cmd = new SqlCommand(cmdStr, _sqlCon);
_sqlCon.Open();
int res = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this._sqlCon.Close();

However, i can't stop wondering - am i, perhaps,
executing the command cmd twice? Once using
ExecuteNonQuery() and the again using Fill(ds)?

If so, i'd like to skip the first one BUT then i
can't see how to obtain the response res from
the database. I've checked the members of da
but i haven't found anything useful.
 
M

Michael C

K Viltersten said:
I run the following code, which works well and gives
me the expected results.

string cmdStr = "select Stuff from There";
SqlCommand cmd = new SqlCommand(cmdStr, _sqlCon);
_sqlCon.Open();
int res = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this._sqlCon.Close();

However, i can't stop wondering - am i, perhaps,
executing the command cmd twice? Once using
ExecuteNonQuery() and the again using Fill(ds)?

If so, i'd like to skip the first one BUT then i
can't see how to obtain the response res from
the database. I've checked the members of da
but i haven't found anything useful.

I'd be suprised if that was calling the database only once. You should fire
up sql profiler and have a look. To get the return value you can add a
return parameter to the command, eg

// code might need a little fixing but something like this:
sqlparameter ret = cmd.Parameters.Add(new sqlparameter("@Return", Int));
ret.Direction = ParameterDirection.ReturnValue;
..... fill etc
int res = (int)ret;

Michael
 
M

Manu

I run the following code, which works well and gives
me the expected results.

string cmdStr = "select Stuff from There";
SqlCommand cmd = new SqlCommand(cmdStr, _sqlCon);
_sqlCon.Open();
int res = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this._sqlCon.Close();

However, i can't stop wondering - am i, perhaps,
executing the command cmd twice? Once using
ExecuteNonQuery() and the again using Fill(ds)?

If so, i'd like to skip the first one BUT then i
can't see how to obtain the response res from
the database. I've checked the members of da
but i haven't found anything useful.

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

You can use the SqlDataAdapter.SelectCommand's StatementCompleted
Event
It had a paramter of type StatementCompletedEventArgs
having a member property RecordCount indicating the number of rows
affected after execution of every statement.
 
K

K Viltersten

I run the following code, which works well and gives
You can use the SqlDataAdapter.SelectCommand's
StatementCompletedEvent. It had a paramter of type
StatementCompletedEventArgs having a member
property RecordCount indicating the number of rows
affected after execution of every statement.

Thanks both of you.
 
M

Michael C

Manu said:
You can use the SqlDataAdapter.SelectCommand's StatementCompleted
Event
It had a paramter of type StatementCompletedEventArgs
having a member property RecordCount indicating the number of rows
affected after execution of every statement.

Oops, I didn't realise ExecuteNonQuery was returning the row count. I guess
there isn't much point getting the row count if you can just get it from the
returned table anyway.
 
I

Ignacio Machin ( .NET/ C# MVP )

I run the following code, which works well and gives
me the expected results.

string cmdStr = "select Stuff from There";
SqlCommand cmd = new SqlCommand(cmdStr, _sqlCon);
_sqlCon.Open();
int res = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this._sqlCon.Close();

However, i can't stop wondering - am i, perhaps,
executing the command cmd twice? Once using
ExecuteNonQuery() and the again using Fill(ds)?

If so, i'd like to skip the first one BUT then i
can't see how to obtain the response res from
the database. I've checked the members of da
but i haven't found anything useful.

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

HI,
You are sending two request to the DB, so its contacted twice. You
reuse the connection though, so it will use one connection only.

Why are you doing the first query though?
 
K

K Viltersten

Ignacio Machin ( .NET/ C# MVP ) said:
HI,
You are sending two request to the DB, so its
contacted twice. You reuse the connection
though, so it will use one connection only.

Why are you doing the first query though?


Because of the initial confusion when i first
wrote the code. Now, a few months later, i'm
revising my code and "mature'ize" it.

So, the questions was rather "do i do that for
a real reason". I'm guessing, not.

Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top