C#: SQL Execute a Stored Procedure with Parameter and fill DataSet
Author: jason
Date: 2019-01-20 14:27:43
Category: Technical

Further building on the previous connecting to SQL statements... Below we are executing a Stored Procedure with a Parameter and placing the results into a DataSet

Code


public static void ConnectDatabaseExecProcedure()
{
try
{
//using System.Data.SqlClient
string connectionString =
@"Data Source=.SQL2017;Initial Catalog=master;Integrated Security=True;Connection TimeOut=30";

using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand command = new SqlCommand("dbo.sp_who2", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@loginame", SqlDbType.VarChar, 25).Value = "sa";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
}
}
catch (Exception e)
{
Console.Write(e);
}
}







jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009