2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by bhagawatshinde
0

Here is a technique for creating a stored procedure in an Access database using C# code.

//using System.Data.Odbc;
      private static bool CreateProc(ref string strError)
      {
         bool blnRetVal = true;

         string strSQL =
            "create proc InsTestNum2(inTestNum) AS INSERT INTO Test_Detail(test_no) values(inTestNum)";

         try
         {
            using (OdbcConnection conn = new OdbcConnection("DSN=DW_ACCESS"))
            {
               conn.Open();
               (new OdbcCommand(strSQL, conn)).ExecuteNonQuery();
               conn.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

This requires the DSN to be registered in the OdbcAdministrator
Control Panel / Administrative Tools / Data Sources (ODBC)

Also, it will need to be customized for your particular database.

This can be called from any type of app with something like this:

//using System.Diagnostics;
         string strError = "";

         if (!CreateProc(ref strError))
         {
            Trace.WriteLine("Could not create proc: " + strError);
            return;
         }

Edited by thines01: formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.