當前位置: 首頁>>代碼示例>>C#>>正文


C# MySqlCommand.AddParameters方法代碼示例

本文整理匯總了C#中MySql.Data.MySqlClient.MySqlCommand.AddParameters方法的典型用法代碼示例。如果您正苦於以下問題:C# MySqlCommand.AddParameters方法的具體用法?C# MySqlCommand.AddParameters怎麽用?C# MySqlCommand.AddParameters使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在MySql.Data.MySqlClient.MySqlCommand的用法示例。


在下文中一共展示了MySqlCommand.AddParameters方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExecuMySQLRDataTable

 /// <summary>
 /// Execute SQL Script and get the DataTable
 /// </summary>
 /// <param name="strSQL">SQL Script</param>
 /// <param name="parms">
 /// The parameters for the SQL command. Each "parameter" should be given as a pair of actual C# parameter where the first one
 /// should be the SQL variable name and the second one is the value for such variable.
 /// </param>
 /// <returns>DataTable</returns>
 public static DataSet ExecuMySQLRDataTable(string strSQL, string DBSource, params object[] parms)
 {
     if (string.IsNullOrEmpty(DBSource))
     {
         if (log.IsErrorEnabled)
         {
             log.Error("The DB Source of Dars can't been found!");
         }
         return null;
     }
     if (string.IsNullOrEmpty(strSQL))
     {
         if (log.IsWarnEnabled)
         {
             log.Warn("The SQL Statement of Dars is error");
         }
         return null;
     }
     int stage = 0;
     try
     {
         using (var conn = new MySqlConnection(DBSource))
         {
             stage = 1;
             conn.Open();
             stage = 2;
             using (var cmd = new MySqlCommand(strSQL, conn))
             {
                 stage = 3;
                 using (var adapter = new MySqlDataAdapter(cmd.AddParameters(parms)))
                 {
                     stage = 4;
                     var dataTable = new DataSet();
                     adapter.Fill(dataTable,"ds");
                     return dataTable;
                 }
             }
         }
     }
     catch (Exception e)
     {
         switch (stage)
         {
             case 0:
                 if (log.IsWarnEnabled)
                 {
                     log.Warn("Init the connection of Dars is error", e);
                     return null;
                 }
                 throw new Exception(e.Message.ToString());
             case 1:
                 if (log.IsWarnEnabled)
                 {
                     log.Warn("Open the connection of Dars is error", e);
                     return null;
                 }
                 throw new Exception(e.Message.ToString());
             case 2:
                 if (log.IsWarnEnabled)
                 {
                     log.Warn("Exeception for executing SQL statement of Dars", e);
                     return null;
                 }
                 throw new Exception(e.Message.ToString());
             case 3:
                 if (log.IsWarnEnabled)
                 {
                     log.Warn("Exeception for creating SQL data adapter of Dars", e);
                 }
                 throw new Exception(e.Message.ToString());
             case 4:
                 if (log.IsWarnEnabled)
                 {
                     log.Warn("Exeception for filling data table of Dars", e);
                 }
                 throw new Exception(e.Message.ToString());
             default:
                 throw new ApplicationException("Bad program flow");
         }
     }
 }
開發者ID:baizu2015,項目名稱:MVCTest,代碼行數:90,代碼來源:MySQLHelper.cs


注:本文中的MySql.Data.MySqlClient.MySqlCommand.AddParameters方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。