0

I'm sharing a snippet from my code below:

string str = "select * from contacts";
DataSet dt = Global.getdatatablefromquery(str);
ExcelGrid.DataSource = dt;
ExcelGrid.DataBind();

I am changing all my queries to stored procedures, but I don't know exactly how will I define my stored procedure in this code? I want something like:

string str = "storedprocedurename";
DataSet dt = Global.getdatatablefromquery(str);
ExcelGrid.DataSource = dt;
ExcelGrid.DataBind();
1
  • 5
    Have you ever used ADO.NET in your Global.getdatatablefromquery method? If so, just change your CommandType to StoredProcedure. Commented Jun 22, 2015 at 5:49

1 Answer 1

2

You have 2 choices (as far as I know):

1. Execute it as Text by specifying EXEC explicitly.

Eg:

cmd = new SqlCommand("EXEC storedprocedurename(@p1, @p2)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

2. You can use CommandType as StoredProcedure

Eg:

cmd = new SqlCommand("storedprocedurename");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

The difference between the 2 approaches is how message pumping happens.(source)

Using the second approach in which the CommandType is explicitly specified as StoredProcedure is more clear and cleaner.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.