Asp.net简单三层+Sqllite 增删改查

时间:2022-06-02 11:04:43
  1. 新建项目à新建一个空白解决方案
  2. 在Model新建一个实体类
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace factory.Model
    7. {
    8.     public class factorys
    9.     {
    10.     //ID INTEGER PRIMARY KEY AUTOINCREMENT
    11.     // NOT NULL
    12.     // DEFAULT 1,
    13.     //correspondent NVARCHAR( 100 ) COLLATE NOCASE,
    14.     //contactaddress NVARCHAR( 100 ) COLLATE NOCASE,
    15.     //contacts NVARCHAR( 50 ) COLLATE NOCASE,
    16.     //contactway NVARCHAR( 50 ) COLLATE NOCASE,
    17.     //contactposition NVARCHAR( 50 ),
    18.     //dutydepartment NVARCHAR( 50 ),
    19.     //dutyofficer NVARCHAR( 50 ) COLLATE NOCASE,
    20.     //note NVARCHAR( 2000 ) COLLATE NOCASE
    21.         private int _ID;
    22.         public int ID
    23.         {
    24.             get { return _ID; }
    25.             set { _ID = value; }
    26.         }
    27.         /// <summary>
    28.         /// 客户单位
    29.         /// </summary>
    30.         private string _correspondent;
    31.         public string Correspondent
    32.         {
    33.             get { return _correspondent; }
    34.             set { _correspondent = value; }
    35.         }
    36.         /// <summary>
    37.         /// 联系地址
    38.         /// </summary>
    39.         private string _contactaddress;
    40.         public string Contactaddress
    41.         {
    42.             get { return _contactaddress; }
    43.             set { _contactaddress = value; }
    44.         }
    45.         /// <summary>
    46.         /// 联系人
    47.         /// </summary>
    48.         private string _contacts;
    49.         public string Contacts
    50.         {
    51.             get { return _contacts; }
    52.             set { _contacts = value; }
    53.         }
    54.         /// <summary>
    55.         /// 联系方式
    56.         /// </summary>
    57.         private string _contactway;
    58.         public string Contactway
    59.         {
    60.             get { return _contactway; }
    61.             set { _contactway = value; }
    62.         }
    63.         /// <summary>
    64.         /// 联系人职务
    65.         /// </summary>
    66.         private string _contactposition;
    67.         public string Contactposition
    68.         {
    69.             get { return _contactposition; }
    70.             set { _contactposition = value; }
    71.         }
    72.         /// <summary>
    73.         /// 负责部门
    74.         /// </summary>
    75.         private string _dutydepartment;
    76.         public string Dutydepartment
    77.         {
    78.             get { return _dutydepartment; }
    79.             set { _dutydepartment = value; }
    80.         }
    81.         /// <summary>
    82.         /// 负责人
    83.         /// </summary>
    84.         private string _dutyofficer;
    85.         public string Dutyofficer
    86.         {
    87.             get { return _dutyofficer; }
    88.             set { _dutyofficer = value; }
    89.         }
    90.         /// <summary>
    91.         /// 备注
    92.         /// </summary>
    93.         private string _note;
    94.         public string Note
    95.         {
    96.             get { return _note; }
    97.             set { _note = value; }
    98.         }
    99.     }
    100. }
  3. 右击解决方案名称à新建一个DAL与BLL ,Model类库,因为个人习惯建好每个类库时喜欢à右击类库属性à默认命名空间将factoryModel改为àfactory.Model所以应用命名空间时为
    1. using factory.Model;
  4. 如果不更改则为
    1. using factoryModel;
  5. 新建一个Windows窗体应用程序(UI层)
  6. 添加引用 :    因为用的是SQLite数据库所以要手动添加一个SQLite的dll引用文件,在解决方案下新建一个lib文件夹将SQLite的dll文件添加进去Asp.net简单三层+Sqllite 增删改查 DAL引用Model,与新建的lib文件下的SQLite.dll 及Asp.net简单三层+Sqllite 增删改查     BLL引用DAL,Model与Model UI引用DAL与Model
  7. DAL下的ado.net SqlliteHelper.cs
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Configuration;
    7. using System.Data.SQLite;
    8. using System.Data;
    9. namespace factory.DAL
    10. {
    11.     public class SqlliteHelper
    12.     {
    13.         //连接字符串
    14.         private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    15.         //方法
    16.         /// <summary>
    17.         /// 增删改 都可以
    18.         /// </summary>
    19.         /// <param name="sql">sql语句</param>
    20.         /// <param name="ps">sql语句中的参数</param>
    21.         /// <returns>返回受影响的行数</returns>
    22.         public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
    23.         {
    24.             try
    25.             {
    26.                 using (SQLiteConnection con = new SQLiteConnection(str))
    27.                 {
    28.                     using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
    29.                     {
    30.                         if (ps != null)
    31.                         {
    32.                             cmd.Parameters.AddRange(ps);
    33.                         }
    34.                         con.Open();
    35.                         return cmd.ExecuteNonQuery();
    36.                     }
    37.                 }
    38.             }
    39.             catch (Exception ex)
    40.             {
    41.                 throw ex;
    42.             }
    43.         }
    44.         /// <summary>
    45.         /// 查询首行首列
    46.         /// </summary>
    47.         /// <param name="sql">sql语句</param>
    48.         /// <param name="ps">参数</param>
    49.         /// <returns>首行首列object</returns>
    50.         public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
    51.         {
    52.             try
    53.             {
    54.                 using (SQLiteConnection con = new SQLiteConnection(str))
    55.                 {
    56.                     using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
    57.                     {
    58.                         con.Open();
    59.                         if (ps != null)
    60.                         {
    61.                             cmd.Parameters.AddRange(ps);
    62.                         }
    63.                         return cmd.ExecuteScalar();
    64.                     }
    65.                 }
    66.             }
    67.             catch (Exception ex)
    68.             {
    69.                 throw ex;
    70.             }
    71.         }
    72.         /// <summary>
    73.         /// 查询的
    74.         /// </summary>
    75.         /// <param name="sql"></param>
    76.         /// <param name="ps"></param>
    77.         /// <returns></returns>
    78.         public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
    79.         {
    80.             SQLiteConnection con = new SQLiteConnection(str);
    81.             try
    82.             {
    83.                 using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
    84.                 {
    85.                     if (ps != null)
    86.                     {
    87.                         cmd.Parameters.AddRange(ps);
    88.                     }
    89.                     try
    90.                     {
    91.                         con.Open();
    92.                         return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    93.                     }
    94.                     catch (Exception ex)
    95.                     {
    96.                         con.Close();
    97.                         con.Dispose();
    98.                         throw ex;
    99.                     }
    100.                 }
    101.             }
    102.             catch (Exception ex)
    103.             {
    104.                 throw ex;
    105.             }
    106.         }
    107.         /// <summary>
    108.         /// 查询的是一个表
    109.         /// </summary>
    110.         /// <param name="sql">sql语句</param>
    111.         /// <param name="ps">sql语句中的参数</param>
    112.         /// <returns>一个表</returns>
    113.         public static DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
    114.         {
    115.             try
    116.             {
    117.                 DataTable dt = new DataTable();
    118.                 using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, str))
    119.                 {
    120.                     if (ps != null)
    121.                     {
    122.                         sda.SelectCommand.Parameters.AddRange(ps);
    123.                     }
    124.                     sda.Fill(dt);
    125.                 }
    126.                 return dt;
    127.             }
    128.             catch (Exception ex)
    129.             {
    130.                 throw ex;
    131.             }
    132.         }
    133.     }
    134. }
  8. App.config配置文件
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <configuration>
    3.   <startup useLegacyV2RuntimeActivationPolicy="true">
    4.     <supportedRuntime version="v4.0"/>
    5.   </startup>
    6.   <connectionStrings>
    7.     <add connectionString="Data Source=factory.db;Version=3;" name="conStr"/>
    8.   </connectionStrings>
    9. </configuration>
  9. SQLite数据库文件就放在UI的binàdebug目录下
  10. 最后说下参数是Asp.net简单三层+Sqllite 增删改查
  11. 完整图片
  12. Asp.net简单三层+Sqllite 增删改查
  13. 这是本人第一次写博客,主要还是以记录为主,如有其它不当之处还望指点
  14.  源码下载 
  15. SQLite第三方编辑工具(sqlitestudio)