Command Execute

  • Thread starter Thread starter JMM B
  • Start date Start date
J

JMM B

In the code bellow, I tried:
this.sqlCommand1.ExecuteScalar();
this.sqlCommand1.ExecuteReader();
this.sqlCommand1.ExecuteNonQuery();
All work! But, What would be the best to choose for the mensioned Stored
Procedure?
thanks.

this.sqlCommand1.Parameters["@CustomerID"].Value = this.textBox1.Text;
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteScalar();
this.label1.Text =
this.sqlCommand1.Parameters["@RETURN_VALUE"].Value.ToString();
this.sqlConnection1.Close();
this.label2.Text =
this.sqlCommand1.Parameters["@CompanyName"].Value.ToString();

ALTER PROCEDURE dbo.CountOrders

(
@CustomerID nchar(5),
@CompanyName nvarchar(40) OUTPUT
)

AS
SET NOCOUNT ON

DECLARE @OrdersCount int

SELECT @CompanyName = Customers.CompanyName, @OrdersCount =
COUNT(Orders.OrderID)
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE (Customers.CustomerID = @CustomerID)
GROUP BY Customers.CompanyName

RETURN @OrdersCount
 
I would use the ExecScalar or ExecNonQuery.
Output parameters can stll be accessed. if you use the ExecNonQuery method,
or you can just return the count in the select statement and use the
ExecScalar method (first row, first col).
 
ExecuteNonQuery.
Don't return any unnecessary rowsets--they're expensive.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Jared said:
I would use the ExecScalar or ExecNonQuery.
Output parameters can stll be accessed. if you use the ExecNonQuery
method, or you can just return the count in the select statement and use
the ExecScalar method (first row, first col).


JMM B said:
In the code bellow, I tried:
this.sqlCommand1.ExecuteScalar();
this.sqlCommand1.ExecuteReader();
this.sqlCommand1.ExecuteNonQuery();
All work! But, What would be the best to choose for the mensioned Stored
Procedure?
thanks.

this.sqlCommand1.Parameters["@CustomerID"].Value = this.textBox1.Text;
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteScalar();
this.label1.Text =
this.sqlCommand1.Parameters["@RETURN_VALUE"].Value.ToString();
this.sqlConnection1.Close();
this.label2.Text =
this.sqlCommand1.Parameters["@CompanyName"].Value.ToString();

ALTER PROCEDURE dbo.CountOrders

(
@CustomerID nchar(5),
@CompanyName nvarchar(40) OUTPUT
)

AS
SET NOCOUNT ON

DECLARE @OrdersCount int

SELECT @CompanyName = Customers.CompanyName, @OrdersCount =
COUNT(Orders.OrderID)
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE (Customers.CustomerID = @CustomerID)
GROUP BY Customers.CompanyName

RETURN @OrdersCount
 
Back
Top