作者总结了一下,使用Winform的三层架构做窗体应用程序,在数据访问方面,有用到纯sql语句方法、参数方法、存储过程方法。

那么什么是三层架构呢?

UI---存放Form窗体---(用户所关心的)

业务逻辑层----存放业务传递(BLL)

数据访问层----底层的数据处理方法(DAL)

数据公共访问方法---SqlHelper(DBUtilty)

使用三层架构设计Winform程序,能够达到”高内聚、低耦合“.团队分工明确,适用于大型的企业级项目

现在我提供一下SQLite数据库的访问方法

第一步:配置数据库。在App.config里

<connectionStrings>
<add name="sqlite" connectionString="data source=|datadirectory|..\..\Hosptial.db;Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite" />
</connectionStrings>

注意:我把数据库文件放在外层了,所以就必须多加【..\..\】,如果放在bin--debug文件加下就要写成|datadirectory|Hosptial.db

第二步:连接配置文件

1.新建一个类SqlHelper.引入命名空间:using System.Data; using System.Data.Sqlite; using System.Configuration;

2.添加引用:引用--添加引用--勾选System.Configuration

3.连接配置文件:在类中添加代码: private static string conn = ConfigurationManager.ConnectionStrings["sqlite"].ToString();

注意:中括号里的["___"]名称必须要与App.Config里的connectionStrings的name相同,否则会出现异常

4.建立公共方法(错误输出到本地的方法,参照我的另一篇博客https://www.cnblogs.com/970728-qi/p/9826476.html)

  • 普通数据访问方法

//1.执行sql语句,并返回受影响的行数,在使用Command向数据库发送增、删、改命令时,通常使用ExecuteNonQuery方法
public static int Update(string sql)
{
SQLiteConnection sqlcon = new SQLiteConnection(conn);
SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
try
{
sqlcon.Open();
return sqlcmd.ExecuteNonQuery();
}
catch (Exception e)
{
PrintError(e.Message);//将错误信息保存到本地
throw new Exception("调用 public static int Update(string sql)方法时出错" + e.Message);
}
finally
{
sqlcon.Close();//关闭练连接
}
}
  //2.执行Sql语句,返回结果集的第一行,第一列。通常使用ExecuteScalar方法
public static object SingResult(string sql)
{
SQLiteConnection sqlcon = new SQLiteConnection(conn);
SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
try
{
sqlcon.Open();
return sqlcmd.ExecuteScalar();
}
catch (Exception e)
{
PrintError(e.Message);
throw new Exception("调用public static object SingResult(string sql)方法时出错:" + e.Message);
}
finally
{
sqlcon.Close();
}
}

        //3.执行sql语句,生成一个包含数据的SQLiteDataReader对象实例
public static SQLiteDataReader GetReader(string sql)
{
SQLiteConnection sqlcon = new SQLiteConnection(conn);
SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
try
{
sqlcon.Open();
return sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
sqlcon.Close();
PrintError(e.Message);
throw new Exception("调用public static SQLiteDataReader GetReader(string sql)方法时出错:" + e.Message);
}
}

//4.产生dataTable对象
public static DataTable GetDataTable(string sql)
{
SQLiteConnection sqlcon = new SQLiteConnection(conn);
SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
SQLiteDataAdapter da = new SQLiteDataAdapter(sqlcmd);//创建适配器对象
DataTable table = new DataTable();
try
{
sqlcon.Open();
da.Fill(table);
return table;
}
catch (Exception e)
{ PrintError(e.Message);
throw new Exception("调用public static DataSet GetDataSet(string sql)方法时出错:" + e.Message);
}
finally
{
sqlcon.Close();
}
}
  • 参数方法

      public static int UpdateParam(string sql, SQLiteParameter[] parameter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.Parameters.AddRange(parameter);
    return sqlcmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
    PrintError(e.Message);
    throw new Exception("调用 public static int UpdateParam(string sql, SQLiteParameter[] parameter)方法时出错" + e.Message);
    }
    finally
    {
    sqlcon.Close();
    }
    } public static object SingleResultParam(string sql, SQLiteParameter[] parameter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.Parameters.AddRange(parameter);
    return sqlcmd.ExecuteScalar();
    }
    catch (Exception e)
    {
    PrintError(e.Message); throw new Exception("调用 public static object SingleResultParam(string sql, SQLiteParameter[] parameter)方法时出错" + e.Message);
    }
    finally
    {
    sqlcon.Close();
    }
    } public static SQLiteDataReader GetReaderParam(string sql, SQLiteParameter[] parameter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(sql, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.Parameters.AddRange(parameter);
    return sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
    }
    catch (Exception e)
    {
    sqlcon.Close();
    PrintError(e.Message);
    throw new Exception("调用public static SQLiteDataReaderParam GetReader(string sql,SQLiteParameter[] parameter)方法时出错" + e.Message);
    }
    }
  • 存储过程方法
          public static int UpdateProce(string spName, SQLiteParameter[] parameter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(spName, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.CommandType = CommandType.StoredProcedure;
    sqlcmd.Parameters.AddRange(parameter);
    return sqlcmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
    PrintError(e.Message);
    throw new Exception("调用public static int UpdateProce(string spName,SQLiteParameter[] parameter)方法出错:" + e.Message);
    }
    finally
    {
    sqlcon.Close();
    } }
    public static object SingleResultProc(string spName, SQLiteParameter[] paramter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(spName, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.CommandType = CommandType.StoredProcedure;
    sqlcmd.Parameters.AddRange(paramter);
    return sqlcmd.ExecuteScalar(); }
    catch (Exception e)
    {
    PrintError(e.Message);
    throw new Exception("调用 public static object SingleResultProc(string spName,SQLiteParameter[] paramter)方法出错:" + e.Message);
    }
    finally
    {
    sqlcon.Close();
    }
    }
    public static SQLiteDataReader GetReaderProc(string spName, SQLiteParameter[] parameter)
    {
    SQLiteConnection sqlcon = new SQLiteConnection(conn);
    SQLiteCommand sqlcmd = new SQLiteCommand(spName, sqlcon);
    try
    {
    sqlcon.Open();
    sqlcmd.Parameters.AddRange(parameter);
    sqlcmd.CommandType = CommandType.StoredProcedure;
    return sqlcmd.ExecuteReader(CommandBehavior.CloseConnection); }
    catch (Exception e)
    {
    sqlcon.Close();
    PrintError(e.Message);
    throw new Exception("调用 public static SQLiteDataReader GetReaderProc(string spName, SQLiteParameter[] parameter)方法出错:" + e.Message);
    } }

    因为会写第一个基本的sqlite语句方法,其他两种都大致相同。作者就不再赘述了,如有错误欢迎指正,如若转载,请标注出处。

基于三层架构下的公共数据访问方法(Sqlite数据库)的更多相关文章

  1. MVC项目实践,在三层架构下实现SportsStore,从类图看三层架构

    在"MVC项目实践,在三层架构下实现SportsStore-02,DbSession层.BLL层"一文的评论中,博友浪花一朵朵建议用类图来理解本项目的三层架构.于是就有了本篇: I ...

  2. MVC项目实践,在三层架构下实现SportsStore-02,DbSession层、BLL层

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  3. MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  4. MVC项目实践,在三层架构下实现SportsStore-08,部署到IIS服务器

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  5. Delphi数据库的三层架构的问题和解决方法

    Delphi数据库的三层架构的问题和解决方法 原创 2014年03月26日 16:26:03 标签: Delphi / 数据库三层架构 / DCOM / DCOMConnection 790 //-- ...

  6. 三层架构下的EntityFramework codefirst

    好久没写博客了,今天研究了EF框架的CodeFirst模式,从字面意思可以看出,代码优先.所谓代码优先,与以往的添加ado.net不同,主要是编写代码生成数据库和数据表,生成数据实体映射.个人感觉这种 ...

  7. MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  8. MVC项目实践,在三层架构下实现SportsStore-04,实现分页

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  9. MVC项目实践,在三层架构下实现SportsStore-05,实现导航

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

随机推荐

  1. Java初始化块

    1.使用初始化块 [修饰符]{ //初始化块的可执行性代码 } 初始化块虽然也是Java类的一种成员,但它没有名字,也就没有标识,因此无法通过类.对象来调用初始化块.初始化块只在创建Java对象时隐式 ...

  2. php 把数组保存为标准的数组格式,存储到文件中

    <?php $file='./test.php'; $array=array('color'=> array('blue','red','green'),'size'=> array ...

  3. Python网络爬虫入门篇

    1.  预备知识 学习者需要预先掌握Python的数字类型.字符串类型.分支.循环.函数.列表类型.字典类型.文件和第三方库使用等概念和编程方法. 2. Python爬虫基本流程 a. 发送请求 使用 ...

  4. spring boot+自定义 AOP 实现全局校验

    最近公司重构项目,重构为最热的微服务框架 spring boot, 重构的时候遇到几个可以统一处理的问题,也是项目中经常遇到,列如:统一校验参数,统一捕获异常... 仅凭代码 去控制参数的校验,有时候 ...

  5. 如何加速GitHub访问速度

    http://tool.chinaz.com/网站中填入assets-cdn.github.com选取响应最小的ip,将ip.域名填入到C:\Windows\System32\drivers\etc下 ...

  6. 王之泰201771010131《面向对象程序设计(java)》第十四周学习总结

    第一部分:理论知识学习部分 第12章 Swing用户界面组件 12.1.Swing和MVC设计模式 a 设计模式初识b 模型—视图—控制器模式c Swing组件的模型—视图—控制器分析 12.2布局管 ...

  7. 大牛的IT经验,方法【跟,帮】

    学习方法一:实践,应用,坚持. [swoole-韩天峰] 我最开始工作也是在2家小公司,后来加入腾讯阿里,主要原因还是我坚持学习基础知识,从而得到了这个机会.有几个方面的基础知识,我建议每一位PHP程 ...

  8. DataNitro安装配置

    必须安装python2,并配置环境变量,第一次安装时没有安装python,第一次打开可以使用,后来就打不开了,卸载重装也不行,安装Python2之后才可以. 按步骤安装就可以 不支持WPS

  9. 显示等待 (web自动化测试)

    from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from sel ...

  10. js斐波那契数列

    斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...... 这个数列从第3项开始,每一项都等于前两项之和. 1.递归算法: function ...