MS SQL Server - Database - access possibilities

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,

I have to query an external database. First, the customer said, I get back a table, then only one result set.

Is there a guideline, example procedure, which applies functions in which case?

Example:

using (SqlCommand cmd = new SqlCommand(StoredProcedureNameDB, GRUPODB))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter RetVal = cmd.Parameters.Add("RetVal", SqlDbType.Int);
RetVal.Direction = ParameterDirection.ReturnValue;
// ....
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
txtResult.Text += rdr[0].ToString() + ", " + rdr[1].ToString() + Environment.NewLine;


counter++;
//break; // Only one record comes back.
}
}
}
catch (SqlException ex)


T GetParamValue<T>(object val, T defaultVal)
{
if (val == DBNull.Value)
return defaultVal;
return (T)val;
}

OpenSQLConnection();
using (var cmd = new SqlCommand("dbo.RequestedProgram", SQL_DB))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Program", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
program = GetParamValue<string>(cmd.Parameters["@Program"].Value, null);

if (program == null)
{
throw new Exception($"class - GetRequestedProgram - @Program is null", program));
}
With best regards Markus

Continue reading...
 
Back
Top