Creating SP in MySQL DB through C# .Net gives error on parameter varia

Creating SP in MySQL DB through C# .Net gives error on parameter varia

Page 1 of 1

1 Replies - 1696 Views - Last Post: 17 August 2010 - 03:14 PM Rate Topic: -----

#1 vcthakkar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 09

Creating SP in MySQL DB through C# .Net gives error on parameter varia

Posted 17 August 2010 - 02:49 PM

Hi,

I have written C# Console Application which fires a SQL DDL statement to create SP in MySQL DB.

I am storing the SQL DDL Statement to create SP in MySQL DB into String variable.

I then assign this string to CommandText property of MySQLCommand variable and then call ExecuteNonQuery method on MySQLCommand variable.

I have set the MySQLConnection object well and opened the connection also well.

Upon execution of above statement, .NET runtime throws MySQLException {"Fatal error encountered during command execution."} with inner exception {"Parameter '@v_limit' must be defined."}

Kindly see the code below.
namespace DBScriptCreator
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Variable Declaration

            String connectionString = string.Empty;
            MySqlConnection connection = null;
            int result = 0;
            String queryString = String.Empty;

            #endregion

            #region Open Connection and Create the Command

            connectionString = ConfigurationManager.ConnectionStrings["InsuranceDevelopment"].ConnectionString.ToString();
            connection = new MySqlConnection(connectionString);
            connection.Open();
            Console.WriteLine("Connection to MySQL : " + connection.State.ToString());

            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;

            #endregion

            #region Execute the Query
            queryString = @"CREATE DEFINER = 'root'@'%'
PROCEDURE GetByInspaction(IN pLmt INT, IN pOff INT, OUT pTotalRow INT)
BEGIN
  SET pTotalRow = (SELECT
    COUNT(*)
  FROM
    Inspaction);
  PREPARE ps FROM '
                SELECT
                    InspactionId ,
                    ClaimRegNo,
                    Date,
                    Subject,
                    DateOfLoss,
                    InspectionBodyText,
                    CattleTaggingId,
                    Age,
                    Mark,
                    Disease,
                    Conclusion,
                    Genuine
                    FROM Inspaction
                    Order by Date
                    LIMIT ? OFFSET ?';

  SET @v_limit = pLmt;
  SET @v_offset = pOff;
  EXECUTE ps USING[b] @v_limit, @v_offset;[/b]
  DROP PREPARE ps;
END
$$";
                    cmd.CommandText = queryString ;
                    result = cmd.ExecuteNonQuery();

            #endregion
                

            cmd = null;
            connection.Close();
            connection = null;
            Console.ReadKey();
        }
    }
}



Kindly look into this and help me get rid of this problem.

Thanks in Advance.

Regards,
Vimal Thakkar

This post has been edited by JackOfAllTrades: 17 August 2010 - 03:08 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Creating SP in MySQL DB through C# .Net gives error on parameter varia

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Creating SP in MySQL DB through C# .Net gives error on parameter varia

Posted 17 August 2010 - 03:14 PM

I think you need to DECLARE your variables before you use them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1