Home Forums Programming General how to run a stored procedure from C# application RE: how to run a stored procedure from C# application

  • Try this. Just paste the code into a button's click event. You'll need to change the items shown bold to match your database settings.

    string oConnString = "Data Source=localhost;Initial Catalog=ALocalDataBase;Integrated Security=True";

    SqlConnection oConn = new SqlConnection(oConnString);

    SqlCommand oCmd = new SqlCommand("Exec sp_AnSProc", oConn);

    SqlDataAdapter oAdptr = new SqlDataAdapter(oCmd);

    DataSet oDS = new DataSet();

    oConn.Open();

    oCmd.ExecuteNonQuery();

    oCmd.CommandTimeout = 600;

    oAdptr.Fill(oDS);

    //uncomment the following line to view the xml representation of the data returned

    //System.Diagnostics.Debug.WriteLine(oDS.GetXml());

    oConn.Close();

    Note: If your stored procedure requires that parameters be passed in, just use String Concatenation.

    Good luck. I hope this helps.

    Software Developer
    I teach; therefore, I learn.