zoukankan      html  css  js  c++  java
  • 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);
    }
    }

    03 2012 档案

     
    摘要: JSM SqlHelper继承了旧的SqlHelper的静态特性,并加以改善,增加了一些web.config配置和对象类的实现,从而提高数据库读写性能,改善代码清晰程度,使代码更容易查阅、维护。本文主要介绍JSM SqlHelper2.0新版本的特性和用法,欢迎大家提出宝贵意见!阅读全文
    posted @ 2012-03-22 12:07 jsm官方BLOG 阅读(1100) | 评论 (8) 编辑
     
    摘要: JSM是一个开源开发团队,是我们计划要上线的官方网站(www.jishu.me)的官方博客。阅读全文
    posted @ 2012-03-21 10:52 jsm官方BLOG 阅读(33) | 评论 (1) 编辑
     
    摘要: JSM SqlHelper 是面象.NET开发环境,用于协助多种数据库操作的高性能和高重用性的完全开源项目!JSM SqlHelper提供SqlServerHelper、AccessHelper、OracleHelper、MySqlHelper开源类,用于方便操作各种数据库开发。官方网站:http://www.jishu.me阅读全文
    posted @ 2012-03-21 10:50 jsm官方BLOG 阅读(124) | 评论 (0) 编辑
  • 相关阅读:
    移动端iOS点击闪烁
    盒子布局(标准)
    zepto.js 自定义打包集成其他模块构建流程
    一种移动端position:absolute布局:
    CSS基础:text-overflow:ellipsis溢出文本显示省略号的详细方法_CSS教程
    ie8 下的半透明 background:rgba 与opacity失效 兼容办法
    在webstorm中编译less,以及压缩css
    JS截取字符串substr 和 substring方法的区别
    array数据处理
    git 使用小结
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2413205.html
Copyright © 2011-2022 走看看