JSM SqlHelper 2.0 新特性(C#)


 本文主要介紹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 源碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM