- 新建项目à新建一个空白解决方案
- 在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)
时间: 2024-10-02 01:38:26