当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.IsConnected方法代码示例

本文整理汇总了C#中SqlConnection.IsConnected方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.IsConnected方法的具体用法?C# SqlConnection.IsConnected怎么用?C# SqlConnection.IsConnected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SqlConnection的用法示例。


在下文中一共展示了SqlConnection.IsConnected方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Bind

 /// <summary>
 /// คืนค่าจากคำสั่ง SQLQuery เป็น DataTable
 /// </summary>
 /// <param name="strSQL"></param>
 /// <returns></returns>
 public DataTable Bind(string strSQL)
 {
     #region Variable
     var csSQL = getConnectionString(cs);
     var dt = new DataTable();
     #endregion
     #region Procedure
     if (!string.IsNullOrEmpty(csSQL))
     {
         if (dbType == DBType.SQLServer)
         {
             #region SQLServer
             using (var myConn_SQL = new SqlConnection(csSQL))
             using (var myDa_SQL = new SqlDataAdapter(QueryFilterByDatabaseType(strSQL), myConn_SQL))
             {
                 if (myConn_SQL.IsConnected())
                 {
                     myDa_SQL.SelectCommand.CommandTimeout = 0;
                     myDa_SQL.Fill(dt);
                     myConn_SQL.Dispose();
                     myDa_SQL.Dispose();
                     if (dt.Rows.Count > 0 && dt != null)
                     {
                         return dt;
                     }
                     else
                     {
                         dt.Dispose();
                         return null;
                     }
                 }
                 else
                 {
                     return null;
                 }
             }
             #endregion
         }
         else if (dbType == DBType.ODBC)
         {
             #region ODBC
             using (var myConn_ODBC = new OdbcConnection(csSQL))
             using (var myDa_ODBC = new OdbcDataAdapter(QueryFilterByDatabaseType(strSQL), myConn_ODBC))
             {
                 myDa_ODBC.Fill(dt);
                 myConn_ODBC.Dispose();
                 myDa_ODBC.Dispose();
                 if (dt.Rows.Count > 0 && dt != null)
                 {
                     return dt;
                 }
                 else
                 {
                     dt.Dispose();
                     return null;
                 }
             }
             #endregion
         }
         else if (dbType == DBType.MySQL)
         {
             #region MySQL
             using(var myConn_MySQL = new MySql.Data.MySqlClient.MySqlConnection(csSQL))
             using (var myDa_MySQL = new MySql.Data.MySqlClient.MySqlDataAdapter(QueryFilterByDatabaseType(strSQL), myConn_MySQL))
             {
                 if (myConn_MySQL.IsConnected())
                 {
                     myDa_MySQL.SelectCommand.CommandTimeout = 0;
                     myDa_MySQL.Fill(dt);
                     myConn_MySQL.Dispose();
                     myDa_MySQL.Dispose();
                     if (dt.Rows.Count > 0 && dt != null)
                     {
                         return dt;
                     }
                     else
                     {
                         dt.Dispose();
                         return null;
                     }
                 }
                 else
                 {
                     return null;
                 }
             }
             #endregion
         }
         else
         {
             return null;
         }
     }
     else
     {
//.........这里部分代码省略.........
开发者ID:oofdui,项目名称:ContactCheckupSync,代码行数:101,代码来源:clsSQL.cs

示例2: Execute

    /// <summary>
    /// Execute คำสั่ง SQL ที่ใช้ในการบันทึกข้อมูล โดยสามารถระบุ SQL Parameter ได้
    /// </summary>
    /// <param name="strSQL">SQL Query</param>
    /// <param name="arrParameter">SQL Parameter (new string[,] { { "?ID", txtTest.Text } })</param>
    /// <param name="strDBType">ชนิดของฐานข้อมูล เช่น sql,odbc,mysql</param>
    /// <param name="appsetting_name">ชื่อตัวแปรที่เก็บ ConnectionString ในไฟล์ AppSetting</param>
    /// <param name="outMessage">ข้อความ เมื่อเกิดข้อผิดพลาด</param>
    /// <returns>True=รันสำเร็จ , False=รันไม่สำเร็จ</returns>
    /// <example>
    /// string outMessage;
    /// clsSQL.Execute("UPDATE webboard_type SET type_name=?NAME WHERE type_id=?ID", new string[,] { { "?ID", txtTest.Text }, { "?NAME", "ใช้ Array 2 มิติ" } }, clsSQL.DBType.MySQL, "cs",out outMessage);
    /// </example>
    public bool Execute(string strSQL, string[,] arrParameter, out string outMessage)
    {
        #region Variable
        var csSQL = getConnectionString(cs);
        var result = false;
        var i = 0;
        outMessage = "";
        #endregion
        #region Procedure
        if (!string.IsNullOrEmpty(csSQL) && arrParameter.Rank == 2)
        {
            if (dbType == DBType.SQLServer)
            {
                #region SQLServer
                var myConn_SQL = new SqlConnection(csSQL);
                if (myConn_SQL.IsConnected())
                {
                    var myCmd_SQL = new SqlCommand(QueryFilterByDatabaseType(strSQL), myConn_SQL);

                    for (i = 0; i < arrParameter.Length / arrParameter.Rank; i++)
                    {
                        myCmd_SQL.Parameters.AddWithValue(arrParameter[i, 0], arrParameter[i, 1]);
                    }

                    try
                    {
                        myConn_SQL.Open();
                        myCmd_SQL.ExecuteNonQuery();
                        myConn_SQL.Close();
                        myCmd_SQL.Dispose();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        outMessage = ex.Message;
                        result = false;
                    }
                    finally
                    {
                        myCmd_SQL.Dispose();
                        myConn_SQL.Close();
                    }
                }
                #endregion
            }
            else if (dbType == DBType.ODBC)
            {
                #region ODBC
                var myConn_ODBC = new OdbcConnection(csSQL);
                var myCmd_ODBC = new OdbcCommand(QueryFilterByDatabaseType(strSQL), myConn_ODBC);

                for (i = 0; i < arrParameter.Length / arrParameter.Rank; i++)
                {
                    myCmd_ODBC.Parameters.AddWithValue(arrParameter[i, 0], arrParameter[i, 1]);
                }

                try
                {
                    myConn_ODBC.Open();
                    myCmd_ODBC.ExecuteNonQuery();
                    myConn_ODBC.Close();
                    myCmd_ODBC.Dispose();
                    result = true;
                }
                catch (Exception ex)
                {
                    outMessage = ex.Message;
                    result = false;
                }
                finally
                {
                    myCmd_ODBC.Dispose();
                    myConn_ODBC.Close();
                }
                #endregion
            }
            else if (dbType == DBType.MySQL)
            {
                #region MySQL
                var myConn_MySQL = new MySql.Data.MySqlClient.MySqlConnection(csSQL);
                if (myConn_MySQL.IsConnected())
                {
                    var myCmd_MySQL = new MySql.Data.MySqlClient.MySqlCommand(QueryFilterByDatabaseType(strSQL), myConn_MySQL);

                    for (i = 0; i < arrParameter.Length / arrParameter.Rank; i++)
                    {
                        myCmd_MySQL.Parameters.AddWithValue(arrParameter[i, 0], arrParameter[i, 1]);
//.........这里部分代码省略.........
开发者ID:oofdui,项目名称:ContactCheckupSync,代码行数:101,代码来源:clsSQL.cs


注:本文中的SqlConnection.IsConnected方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。