Stored Procedure return records

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

I have a SP:

CREATE PROCEDURE dbo.users_name
as
select name
from users
order by name

Here's my C# code:
public string[] GetUserNames()
{
SqlCommand objCommand = new SqlCommand("users_name", _Conn);
objCommand.CommandType = CommandType.StoredProcedure;
_Conn.Open();
objCommand.Connection.Close();
objCommand.Connection.Open();
objCommand.ExecuteNonQuery();

// how so I assign the names from the stored procedure to the return array
of string?

_Conn.Close();
}
 
You don't want to use ExecuteNonQuery as that returns no resultset. Use
ExecuteReader or DataAdapter.Fill to fill a DataSet. Then just iterate over
the rows, assigning the correct column value to your array.
Peter
 
Back
Top