I am using winforms in C# to launch a procedure that is stored in my MS SQL Server database. I only have one variable and it is @XmlStr. I have a text box that is going to include the variable and a button I want to start the procedure. Can anyone help me do this? I have been researching this all day, and so far have come up with nothing that has worked for me.
Asked
Active
Viewed 9,349 times
-2
-
1Which solutions did you come by that didn't work for you? – Andrey Jul 08 '15 at 17:28
-
It would help if you told us if and how your stored procedure returns results; output parameters? Selects? – Dour High Arch Jul 08 '15 at 17:41
-
All the stored procedure does is import the XML to a few SQL tables. The text box is for copying and pasting XML to be inserted into a database. – Austin Jul 08 '15 at 17:44
-
I used a number of different sites. Here are a few of the ones I have looked at. http://stackoverflow.com/questions/15918674/execute-a-stored-procedure-inside-textbox-to-get-a-localized-text http://stackoverflow.com/questions/18326946/storedprocedure-in-c-passing-parameters-from-text-boxes http://www.dreamincode.net/forums/topic/329082-how-to-execute-stored-procedure-in-winforms/page__st__0%26 http://forums.asp.net/t/716548.aspx?Binding+Stored+Procedure+OUTPUT+parameters+to+a+TextBox+How+ – Austin Jul 08 '15 at 17:46
-
@Austin how about this one? http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-within-c-sharp-program – Andrey Jul 08 '15 at 17:48
-
@Andrey I still do not understand how to do it from that thread. – Austin Jul 08 '15 at 19:47
-
@Austin I would recommend you to read a book about development for .net. – Andrey Jul 08 '15 at 21:10
2 Answers
5
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("storedProcedureName", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@XmlStr", XmlStrVariable);
cmd.ExecuteNonQuery();
}
This should get you started. Research SqlConnection and SqlCommand for more information.

oppassum
- 1,746
- 13
- 22
1
Hope this helps:
string connectionString = "YourConnectionString";
int parameter = 0;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "NameOfYourStoredProcedure";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("ParameterName", parameter);
try
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// Read your reader data
}
}
}
catch
{
throw;
}
}

msmolcic
- 6,407
- 8
- 32
- 56