新建項目à新建一個空白解決方案 在Model新建一個實體類 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; name...
- 新建項目à新建一個空白解決方案
-
在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目錄下
- 最後說下參數是
- 完整圖片