SQLite 创建数据库插入数据库数据DEMO
注意事项:SQLsugar版本(4.9.9.11) and SQLite版本(1.0.113.0) and System.Data.SQLite.EF6 版本(1.0.113.0)
and System.Data.SQLite.Linq(1.0.113.0) and .NET版本(4.0)
using Sqlite_AND_SqlSuager.Entity;
using Sqlite_AND_SqlSuager.SqlDB;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sqlite_AND_SqlSuager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlSugarClient _db = SqliteDB.GetDB();
List<User> _lstClass = new List<User>();
private void 创建数据库_Click(object sender, EventArgs e)
{
try
{
SqliteDB.CreateDB(true);
textBox1.Text += "创建数据库完成!" + System.Environment.NewLine; ;
}
catch (Exception ex)
{
textBox1.Text += "数据库创建失败!" + ex.Message + System.Environment.NewLine;
}
}
private void 查询数据_Click(object sender, EventArgs e)
{
try
{
_lstClass = new List<User>();
_lstClass = _db.Queryable<User>().ToList();
dataGridView1.DataSource = _lstClass;
}
catch (Exception ex)
{
textBox1.Text += "数据查询失败!" + ex.Message + System.Environment.NewLine;
}
}
private void 插入数据_Click(object sender, EventArgs e)
{
try
{
_lstClass = new List<User>();
for (int i = 0; i < 100; i++)
{
User _mc = new User();
_mc._name = "lh" + i;
_mc.Age = 18;
_mc.Password = "admin";
_mc.Money = 1+i;
_mc.Mdatetime = DateTime.Now;
_lstClass.Add(_mc);
}
_db.Insertable(_lstClass).ExecuteCommand();
textBox1.Text += "数据增加成功!" + System.Environment.NewLine;
}
catch (Exception ex)
{
textBox1.Text += "数据删除失败!" + ex.Message + System.Environment.NewLine;
}
}
private void 删除数据_Click(object sender, EventArgs e)
{
try
{
List<int> _lstID = new List<int>();
int _deleNum = -1;
if (dataGridView1.CurrentRow.Index >= 0)
{
StringBuilder _sbMsg = new StringBuilder("确定删除吗?\r\n");
DialogResult result = MessageBox.Show(_sbMsg.ToString(), "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--)
{
int _iTagID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells["Id"].Value);
_lstID.Add(_iTagID);
}
if (_lstID.Count > 0)
{
_deleNum = _db.Deleteable<User>().In(_lstID).ExecuteCommand();
}
查询数据_Click(null, null);
textBox1.Text += "删除成功!" + _deleNum + System.Environment.NewLine;
}
else
{
textBox1.Text += "选择放弃删除!" + System.Environment.NewLine;
}
}
}
catch (Exception ex)
{
textBox1.Text += "删除失败!" + ex.Message + System.Environment.NewLine;
}
}
}
}
界面代码上面。
下面是SqliteDB类
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sqlite_AND_SqlSuager.SqlDB
{
public partial class SqliteDB
{
public static SqlSugarClient GetDB()
{
string _sConnPath = string.Format("{0}\\DB", Application.StartupPath);
if (!System.IO.Directory.Exists(_sConnPath)) System.IO.Directory.CreateDirectory(_sConnPath);
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = string.Format("DataSource={0}\\user.dat", _sConnPath),
DbType = DbType.Sqlite,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
db.Ado.IsEnableLogEvent = false;
return db;
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="_IsReCreate"></param>
public static void CreateDB(bool _IsReCreate = false)
{
try
{
System.Data.SQLite.SQLiteConnection.ClearAllPools();
GC.Collect();
GC.WaitForPendingFinalizers();
string _sConnPath = string.Format("{0}\\DB", Application.StartupPath);
if (!System.IO.Directory.Exists(_sConnPath)) System.IO.Directory.CreateDirectory(_sConnPath);
string _sFileName = string.Format("{0}\\DB\\demo.dat", Application.StartupPath);
if (_IsReCreate && File.Exists(_sFileName)) File.Delete(_sFileName);
if (_IsReCreate || !File.Exists(_sFileName))
{
var _db = GetDB();
_db.CodeFirst.InitTables(typeof(Entity.User));
_db.Ado.ExecuteCommand("create index if not exists idx_Alarms_State on Alarms(State DESC)");
_db.Ado.ExecuteCommand("create index if not exists idx_Alarms_AlarmDate on Alarms(AlarmDate DESC)");
_db.Ado.ExecuteCommand("CREATE INDEX if not exists idx_Alarms_AlarmDate_State ON Alarms (State DESC, AlarmDate DESC);");
//User usr = new User() { UserName = "Admin", Password = "1", Power = 0 };
//_db.Insertable<User>(usr).ExecuteCommand();
}
}
catch (Exception ex)
{
}
}
}
}
下面是User 类
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sqlite_AND_SqlSuager.Entity
{
public class User
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true)]
public int ID { get; set; }
public string _name { get; set; }
private string _password;//string
private int _age;//double
private decimal _money;//double
//private float _mfloat;//float不支持此数据类型
private DateTime _mdatetime;//datetime
public string Password
{
get
{
return _password;
}
set
{
_password = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public decimal Money
{
get
{
return _money;
}
set
{
_money = value;
}
}
public DateTime Mdatetime
{
get
{
return _mdatetime;
}
set
{
_mdatetime = value;
}
}
}
}
两个DLL
https://download.csdn.net/download/qq_36074218/19992042
https://download.csdn.net/download/qq_36074218/19992044