return data from database

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

I'm using this store procedure to return data from sql server.
SqlDataReader sqlRead = null;

System.Data.SqlClient.SqlCommand ProfileBLOBSelect = new
System.Data.SqlClient.SqlCommand();

ProfileBLOBSelect.CommandText = "[dbo].[ReceptiSearchNaziv]";

ProfileBLOBSelect.CommandType = System.Data.CommandType.StoredProcedure;

ProfileBLOBSelect.Connection = db.sqlConnection;

ProfileBLOBSelect.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Grupa",
System.Data.SqlDbType.NVarChar,50 ));

ProfileBLOBSelect.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Naziv", System.Data.SqlDbType.NVarChar,
50));

ProfileBLOBSelect.Parameters["@Grupa"].Value = table;

ProfileBLOBSelect.Parameters["@Naziv"].Value = naziv;

sqlRead = ProfileBLOBSelect.ExecuteReader();

if (sqlRead.Read())

{

-- What should I put here?

}



How to put data into grid?
 
The code can be written as



SqlDataReader sqlRead = null;

System.Data.SqlClient.SqlCommand ProfileBLOBSelect = new
System.Data.SqlClient.SqlCommand();

ProfileBLOBSelect.CommandText = "[dbo].[ReceptiSearchNaziv]";

ProfileBLOBSelect.CommandType = System.Data.CommandType.StoredProcedure;

ProfileBLOBSelect.Connection = db.sqlConnection;

ProfileBLOBSelect.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Grupa",
System.Data.SqlDbType.NVarChar,50 ));

ProfileBLOBSelect.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Naziv", System.Data.SqlDbType.NVarChar,
50));

ProfileBLOBSelect.Parameters["@Grupa"].Value = table;

ProfileBLOBSelect.Parameters["@Naziv"].Value = naziv;



DataSet resultSet = new DataSet();

DataTable resultTable = new DataTable();

resultTable.Columns.Add("ID",typeof (Guid ));

resultTable.Columns.Add("GoalName", typeof(DateTime));


sqlRead = ProfileBLOBSelect.ExecuteReader();

while (sqlRead.Read())

{
resultTable.Rows.Add(sqlRead.GetGuid(0), sqlRead.GetDateTime(1));
}



resultSet.Tables.Add(resultTable);


The above code first creates an table having columns similar to the output
of the query. Then we apply a while loop and loop through all the rows of
the query and then put that in the dataset.

Reshma
 
Hi,

Are you expecting more than one rows?
If so do somethig like:


SqlDataReader reader = DataProvider.ExecuteReader( cmd);
while( reader.Read() )
{
document = new Document();
document.comment = "";
document.origname = reader["OrigName"].ToString();
document.dDate = Convert.ToDateTime( reader["dDate"]);

DocumentList.Add( document); // this is a collection
}
 

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

Back
Top