H
Hrvoje Voda
I'm reading information from store procedure and I would like to put the
result in dataGrid.
In all examples and books about working with store procedures is the example
only with console.
I'm using windows form.
My code:
void SearchSastojci(string sastojci)
{ SqlConnection conn = null;
SqlDataReader rdr = null;
ReceptiDb.RcptDb db = null;
string sConnectionString = "packet size=4096;user id=sa;persist security
info=False;initial catalog=Recepti";
try
{
conn = new
SqlConnection(sConnectionString);
conn.Open() ;
db = new RcptDb(sConnectionString) ;
SqlCommand cmd = new SqlCommand(
"Names", conn);
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(
new SqlParameter("@Group", Group.Text ));
cmd.Parameters.Add(
new SqlParameter("@Name", textBoxName.Text ) );
// execute the command
rdr = cmd.ExecuteReader();
// iterate through results, printing each to console
while (rdr.Read())
{
grid.AddColumn("Name", "Name") ;
grid.AddColumn("Group", "Group") ;
----- Here I need to read rdr into grid ----- ???
}
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
Hrcko
result in dataGrid.
In all examples and books about working with store procedures is the example
only with console.
I'm using windows form.
My code:
void SearchSastojci(string sastojci)
{ SqlConnection conn = null;
SqlDataReader rdr = null;
ReceptiDb.RcptDb db = null;
string sConnectionString = "packet size=4096;user id=sa;persist security
info=False;initial catalog=Recepti";
try
{
conn = new
SqlConnection(sConnectionString);
conn.Open() ;
db = new RcptDb(sConnectionString) ;
SqlCommand cmd = new SqlCommand(
"Names", conn);
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(
new SqlParameter("@Group", Group.Text ));
cmd.Parameters.Add(
new SqlParameter("@Name", textBoxName.Text ) );
// execute the command
rdr = cmd.ExecuteReader();
// iterate through results, printing each to console
while (rdr.Read())
{
grid.AddColumn("Name", "Name") ;
grid.AddColumn("Group", "Group") ;
----- Here I need to read rdr into grid ----- ???
}
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
Hrcko