-2

In VB.net I can simply execute a stored procedure in SQL Server 2008 using the query in image below, but in C# I got an error.

Can you help me about the proper syntax in C#?

Thanks

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1
using (SqlConnection conn = new SqlConnection(your-connection-string)) {
conn.Open();

// 1.  create a command object identifying the stored procedure
SqlCommand cmd  = new SqlCommand("your-procedure-name", conn);

// 2. set the command object so it knows to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@Username", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));

// execute the command
using (SqlDataReader result = cmd.ExecuteReader()) {
    // iterate through results, printing each to console
    while (result .Read())
    {
      // Name  and Password Should Match with your proc col name 
      var userName  = result["Name"].toString();
      var password  = result["password"].toString();

    }
}
 }

For More details Please Read this this

How to execute a stored procedure within C# program