MySQL .NET parameters in query


 

This example shows you how to run a parameterized query against MySQL. The trick is to remember the ? instad of @ - which is the case of SQL Server.



        public System.Data.DataSet Customer_Open(string sConnString, long ld)
        {
            using (MySqlConnection oConn = new MySqlConnection(sConnString))
            {
                oConn.Open();

                MySqlCommand oCommand = oConn.CreateCommand();
                oCommand.CommandText = "select * from cust_customer where id=?id";

                MySqlParameter oParam = oCommand.Parameters.Add("?id", MySqlDbType.Int32);
                oParam.Value = ld;

                oCommand.Connection = oConn;
                DataSet oDataSet = new DataSet();
                MySqlDataAdapter oAdapter = new MySqlDataAdapter();
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(oDataSet);
                oConn.Close();
                return oDataSet;
            }
        }