JSM SqlHelper 2.0 新特性(C#)

來源:互聯網
上載者:User
文章目錄
  •       執行Sql語句 
  •        TableFramework實現資料加簡單的插入更新

 本文主要介紹JSM SqlHelper2.0新版本的特性和用法,歡迎大家提出寶貴意見!

JSM SqlHelper2.0新特性
  1. 繼承了原SqlHelper的靜態方法模式並加以最佳化。

  2. 增強web.config配置支援,以方便網站的日常維護。

  3. 增加面象對象類,使用SqlHelper對象可以輕鬆實現複雜的程式邏輯。

  4. 增加對Access、Oracle、MySql資料庫支援。

  5. 增加TableFramework類,用於實現簡單的Insert和Update語句,自動產生參數和Sql語句,減少代碼量。

JSM SqlHelper 配置方法

      開啟web.config檔案,配置configuration節點下的configurationSettings中的add項,其中name標識為程式預設的讀取連結地址,如果不想在

web.config中讀取,可以重寫和使用方法傳入方式導入連結字串。web.config配置執行個體代碼如下:

   <?xml version="1.0"?>
<configuration>
<connectionStrings>
<add connectionString="server=.;uid=sa;pwd=***;database=dbname" name="SqlServerHelper"/>
<add connectionString="Data Source=orcl;User Id=system;Password=***;Integrated Security=no" name="OracleHelper"/>
<add connectionString="server=localhost;uid=root;pwd=***;database=mysql_dbname" name="MySqlHelper"/>
</connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
JSM SqlHelper靜態實現方法
    /// <summary>
/// 讀取學生資訊
/// </summary>
DataTable ReadStudent(long stid)
{
return SqlServerHelper.ReadTable("select * from [Students] where stid=@stid",
SqlServerHelper.CreateInputParameter("@stid", SqlDbType.BigInt, stid));
}

/// <summary>
/// 刪除學生資訊
/// </summary>
int DeleteStudent(long stid)
{
return SqlServerHelper.ExecuteNonQuery("delete from [Students] where stid=@stid",
SqlServerHelper.CreateInputParameter("@stid", SqlDbType.BigInt, stid));
}
JSM SqlHelper面象對象實現方法

       面象對象方法在複雜的資料庫操作過程中,可以實現在一次串連資料庫執行多個SQL代碼,從而提高程式的執行效率。

      執行Sql語句 
    /// <summary>
/// 讀取學生資訊
/// </summary>
DataTable ReadStudent(long stid)
{
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.Command.CommandText = "select * from [Students] where stid=@stid";
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
            helper.Open();
return helper.ReadTable();
}
}

/// <summary>
/// 刪除學生資訊 實現事務和執行刪除SQL語句
/// </summary>
int DeleteStudent(long stid)
{
using (SqlServerHelper helper = new SqlServerHelper())
{
           helper.Open();
//啟動事務
SqlTransaction tran = helper.Connection.BeginTransaction();
helper.Command.Transaction = tran;
try
{
helper.Command.CommandText = "delete from [Students] where stid=@stid";
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
int r= helper.ExecuteNoneQuery();
tran.Commit();
return r;
}
catch {
tran.Rollback();
throw;
}
}
}
       TableFramework實現資料加簡單的插入更新

         使用TableFramework可以通過Tameframework添加列和資料值資訊,使用InsertTable或UpdateTable方法輕鬆實SQL語句的產生和參數產生,準確而簡單,並且容易日後維護,添加更只需添加一個列值即可。舉例代碼如下:

    /// <summary>
/// 新增學生資訊
/// </summary>
bool InsertStudent(string studentName, string className)
{
TableFramework tf = new TableFramework("students");
tf.Add("student_name", studentName);
tf.Add("class", className);
using (SqlServerHelper helper = new SqlServerHelper())
{
            helper.Open();
//執行插入新的記錄
return helper.InsertTable(tf);
}
}
/// <summary>
/// 更新學生資訊
/// </summary>
bool UpdateStudent(long stid,string studentName, string className)
{
TableFramework tf = new TableFramework("students");
tf.Add("student_name",studentName);
tf.Add("class", className);
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
            helper.Open();
return helper.UpdateTable(tf, "where stid=@stid", false);
}
}

 JSM SqlHelper 2.0 源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.