Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I want to execute a sql stored procedure in C#. The procedure just inserts data into a table, therefore it has no parameters. I know how to do this,no sure what goes after the connection is open. :(


C#
string connectionString = "connect string...etc";

            
           string sqlstring = "execute myprocedure";

            OleDbConnection connection = new OleDbConnection(connectionString);

            OleDbCommand command = new OleDbCommand(sqlstring,connection);
          
     
            command.CommandType = CommandType.StoredProcedure;

            try 
              {
               connection.open();

                ??????????????????????

               connection.close();
                }

           catch (exception)



Please help.

nicole
Posted
Updated 15-May-12 23:03pm
v2
Comments
Code 89 16-May-12 5:18am    
cmd.ExecuteNonQuery();

 
Share this answer
 
1.
This is wrong!
sqlstring = "execute myprocedure";

If you plan to use CommandType.StoredProcedure it should be:
sqlstring = "myprocedure";


2.
Try this:
C#
using(OleDbConnection connection = new OleDbConnection(connectionString))
{
    OleDbCommand command = new OleDbCommand("myprocedure",connection);
    command.CommandType = CommandType.StoredProcedure;
 
    try 
    {
         connection.open();
         command.ExecuteNonQuery();           
    }
    catch (OledbExcpetion olex)
    {
         Debug.WriteLine(olex.ToString());
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.ToString());        
    }
    finally
    {
        //if you are using try/catch block
        //this is better place for default connection closing
        connection.Close(); 
    }
}
 
Share this answer
 
Comments
nikki88 17-May-12 3:24am    
Thank you :) that was exactly the problem... works perfectly now
Just execute the Command:
C#
command.ExecuteNonQuery();


It will Execute Procedure if your procedure does not require Parameters.
 
Share this answer
 
v2
Comments
nikki88 16-May-12 5:30am    
I tried it and it gives me an exception at that line. "Unspecified error"
C#
Between Con.open() and con.close u try like this it will work
          try
          {
              connCmd = new OledbCommand(strSQL, dbConn);
              int i = connCmd.ExecuteNonQuery();
              if (i >= 1)
              {
                  //egreen
                  lblMessage.Text = "Suucess ";
              }
              else
              {
                  lblMessage.Text = "Fail ";
              }

          }
          finally
          {

          }
 
Share this answer
 
C#
string connectionString = "connect string...etc";
             
           string sqlstring = "execute myprocedure";
           OleDbConnection connection = new OleDbConnection(connectionString);
           OleDbCommand command = new OleDbCommand(sqlstring,connection);
           command.CommandType = CommandType.StoredProcedure;
 
            try 
              {
               if (connection!= null && connection.State == ConnectionState.Open) { connection.Close(); }
else{
               connection.open();
 }
               command.executeNonQuery();
 
               connection.close();
                }
 
           catch (exception){}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900