Asp.net简单三层+Sqllite 增删改查
2018-06-23 23:13:46来源:未知 阅读 ()
- 新建项目à新建一个空白解决方案
-
在Model新建一个实体类
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
-
namespace factory.Model
-
{
-
public class factorys
-
{
-
//ID INTEGER PRIMARY KEY AUTOINCREMENT
-
// NOT NULL
-
// DEFAULT 1,
-
//correspondent NVARCHAR( 100 ) COLLATE NOCASE,
-
//contactaddress NVARCHAR( 100 ) COLLATE NOCASE,
-
//contacts NVARCHAR( 50 ) COLLATE NOCASE,
-
//contactway NVARCHAR( 50 ) COLLATE NOCASE,
-
//contactposition NVARCHAR( 50 ),
-
//dutydepartment NVARCHAR( 50 ),
-
//dutyofficer NVARCHAR( 50 ) COLLATE NOCASE,
-
//note NVARCHAR( 2000 ) COLLATE NOCASE
-
private int _ID;
-
-
public int ID
-
{
-
get { return _ID; }
-
set { _ID = value; }
-
}
-
-
/// <summary>
-
/// 客户单位
-
/// </summary>
-
private string _correspondent;
-
-
public string Correspondent
-
{
-
get { return _correspondent; }
-
set { _correspondent = value; }
-
}
-
-
/// <summary>
-
/// 联系地址
-
/// </summary>
-
private string _contactaddress;
-
-
public string Contactaddress
-
{
-
get { return _contactaddress; }
-
set { _contactaddress = value; }
-
}
-
-
/// <summary>
-
/// 联系人
-
/// </summary>
-
private string _contacts;
-
-
public string Contacts
-
{
-
get { return _contacts; }
-
set { _contacts = value; }
-
}
-
-
/// <summary>
-
/// 联系方式
-
/// </summary>
-
private string _contactway;
-
-
public string Contactway
-
{
-
get { return _contactway; }
-
set { _contactway = value; }
-
}
-
-
/// <summary>
-
/// 联系人职务
-
/// </summary>
-
private string _contactposition;
-
-
public string Contactposition
-
{
-
get { return _contactposition; }
-
set { _contactposition = value; }
-
}
-
-
/// <summary>
-
/// 负责部门
-
/// </summary>
-
private string _dutydepartment;
-
-
public string Dutydepartment
-
{
-
get { return _dutydepartment; }
-
set { _dutydepartment = value; }
-
}
-
-
/// <summary>
-
/// 负责人
-
/// </summary>
-
private string _dutyofficer;
-
-
public string Dutyofficer
-
{
-
get { return _dutyofficer; }
-
set { _dutyofficer = value; }
-
}
-
-
/// <summary>
-
/// 备注
-
/// </summary>
-
private string _note;
-
-
public string Note
-
{
-
get { return _note; }
-
set { _note = value; }
-
}
-
-
-
}
-
}
-
-
右击解决方案名称à新建一个DAL与BLL ,Model类库,因为个人习惯建好每个类库时喜欢à右击类库属性à默认命名空间将factoryModel改为àfactory.Model所以应用命名空间时为
-
using factory.Model;
-
-
如果不更改则为
-
using factoryModel;
-
- 新建一个Windows窗体应用程序(UI层)
- 添加引用 : 因为用的是SQLite数据库所以要手动添加一个SQLite的dll引用文件,在解决方案下新建一个lib文件夹将SQLite的dll文件添加进去 DAL引用Model,与新建的lib文件下的SQLite.dll 及 BLL引用DAL,Model与Model UI引用DAL与Model
-
DAL下的ado.net SqlliteHelper.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Configuration;
-
using System.Data.SQLite;
-
using System.Data;
-
namespace factory.DAL
-
{
-
public class SqlliteHelper
-
{
-
//连接字符串
-
private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
-
-
//方法
-
/// <summary>
-
/// 增删改 都可以
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">sql语句中的参数</param>
-
/// <returns>返回受影响的行数</returns>
-
public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
using (SQLiteConnection con = new SQLiteConnection(str))
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
con.Open();
-
return cmd.ExecuteNonQuery();
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
-
}
-
/// <summary>
-
/// 查询首行首列
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">参数</param>
-
/// <returns>首行首列object</returns>
-
public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
using (SQLiteConnection con = new SQLiteConnection(str))
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
con.Open();
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
return cmd.ExecuteScalar();
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
-
}
-
-
/// <summary>
-
/// 查询的
-
/// </summary>
-
/// <param name="sql"></param>
-
/// <param name="ps"></param>
-
/// <returns></returns>
-
public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
-
{
-
SQLiteConnection con = new SQLiteConnection(str);
-
try
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
try
-
{
-
con.Open();
-
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
-
}
-
catch (Exception ex)
-
{
-
con.Close();
-
con.Dispose();
-
throw ex;
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
-
throw ex;
-
}
-
-
}
-
-
/// <summary>
-
/// 查询的是一个表
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">sql语句中的参数</param>
-
/// <returns>一个表</returns>
-
public static DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
DataTable dt = new DataTable();
-
using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, str))
-
{
-
if (ps != null)
-
{
-
sda.SelectCommand.Parameters.AddRange(ps);
-
-
}
-
sda.Fill(dt);
-
-
}
-
return dt;
-
}
-
catch (Exception ex)
-
{
-
-
throw ex;
-
}
-
-
}
-
-
}
-
}
-
-
App.config配置文件
-
<?xml version="1.0" encoding="utf-8" ?>
-
<configuration>
-
<startup useLegacyV2RuntimeActivationPolicy="true">
-
-
<supportedRuntime version="v4.0"/>
-
-
</startup>
-
-
<connectionStrings>
-
-
<add connectionString="Data Source=factory.db;Version=3;" name="conStr"/>
-
</connectionStrings>
-
</configuration>
-
- SQLite数据库文件就放在UI的binàdebug目录下
- 最后说下参数是
- 完整图片
- 这是本人第一次写博客,主要还是以记录为主,如有其它不当之处还望指点
- 源码下载
- SQLite第三方编辑工具(sqlitestudio)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- [01]HTML基础之简单介绍 2020-06-01
- HTML开发实例-简单相亲网站开发(主体为table) 2020-05-27
- Bootstrap4网格系统+文字排版+颜色 简单练习 2020-04-14
- 简单理解vertical-align属性和基线 2020-04-03
- Cookie SameSite属性介绍及其在ASP.NET项目中的应用 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash