Chris Ongsuco's Weblog
Information Technology, business, life, food...

Command.ExecuteRow()

Tuesday, February 08, 2005 3:08 AM
ExecuteRow is a new method in .net that will return 1 record from the query. Here is a sample from .Net Beta 1:

ConnectionStringSettingsCollection cssc = ConfigurationSettings.ConnectionStrings;
string connectionstring = cssc["Main"].ConnectionString;
string sql = "select * from Customers where CustomerId = 'ANTON' "; // Using Northwind Database

SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
SqlRecord record = cmd.ExecuteRow();

Response.Write(record["ContactName"].ToString());
conn.Close();

NOTE: queries without the WHERE condition will return the first record.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Command.ExecuteRow()

I was using (and liked) this object, but it was dropped in Beta 2.
Wonder why? 4/20/2005 9:38 PM | Longview

# re: Command.ExecuteRow()

oh really?! I havent peeked on Beta 2 yet...just dont have the time right now...but I am wondering why also... 4/21/2005 3:07 AM | Chris

# re: Command.ExecuteRow()

I was using too. Now I''m using SqlDataReader which is filled with sqlcommand who has behavior set on single row.

example:
dr = cmd.ExecuteReader(CommandBehavior.SingleRow);

It is almost the same like in beta 1, only here you have to type more...

In Beta 1 sqlRecord was very usefull and i don't know why they removed it??? 5/17/2005 2:59 PM | Pavle

# re: Command.ExecuteRow()

SqlRecord record = cmd.ExecuteRow();
SqlConnection sc = new SqlConnection(ss);

//string count = "select count(*) from emp_dtls";
//SqlCommand scmd = new SqlCommand(count, sc);

//Response.Write("Count Of Records="+scmd.ExecuteScalar().ToString ());
//sc.Close();


string sql = "select * from Emp_dtls where Empname = 'kamal' "; // Using Northwind Database


SqlCommand cmd = new SqlCommand(sql,sc);


sc.Open();
SqlRecord record = cmd.ExecuteRow();
--gettig error above line---

Response.Write(record["ContactName"].ToString());
sc.Close.Close();
11/18/2008 10:43 PM | kamal

Post a comment