IDE:VS2017 community
sqlite增、删、改、查:
using System; using System.Collections.Generic; using System.Data.sqlite; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Hellosqlite { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { string DBPath = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"Hellosqlite.db"; private sqliteConnection DBConnection = null; public MainWindow() { InitializeComponent(); } private void tbsqliteOpen_Click(object sender,RoutedEventArgs e) { DBConnection = new sqliteConnection(DBPath); DBConnection?.Open(); } private void tbsqliteCreateTable_Click(object sender,RoutedEventArgs e) { int ret = -1; string sql; bool tableExists = false; sqliteCommand cmd = new sqliteCommand(); cmd.Connection = DBConnection; /* 表是否存在 */ sql = "SELECT * FROM sqlite_master WHERE type='table' and name='persons'"; cmd.CommandText = sql; using (sqliteDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { tableExists = true; } } /* 表不存在,则创建表 */ if (!tableExists) { sql = "CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY,name VARCHAR(20),age INTEGER,address VARCHAR(100),data BLOB);"; cmd.CommandText = sql; ret = cmd.ExecuteNonQuery(); Console.WriteLine($"创建表返回: {ret}"); } } private void tbsqliteInsert_Click(object sender,RoutedEventArgs e) { int ret = -1; string sql; byte[] datas = new byte[] { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; sqliteCommand cmd = new sqliteCommand(); cmd.Connection = DBConnection; /* 插入一条数据 */ #if DEBUG // 方式一 sql = $"INSERT INTO persons(id,name,age,address,data) VALUES (@id,@name,@age,@address,@data)"; cmd.CommandText = sql; cmd.Parameters.Add(new sqliteParameter("@id",2)); cmd.Parameters.Add(new sqliteParameter("@name","王五")); cmd.Parameters.Add(new sqliteParameter("@age",12)); cmd.Parameters.Add(new sqliteParameter("@address","中国北京")); cmd.Parameters.Add(new sqliteParameter("@data",datas)); #else // 方式二 sql = string.Format($"INSERT INTO persons(id,address) VALUES ('{1000}','{"张三"}','{20}','{"中国-广东深圳坂田"}')"); cmd.CommandText = sql; #endif ret = cmd.ExecuteNonQuery(); Console.WriteLine($"{ret}行被插入!"); } private void tbsqliteDelete_Click(object sender,RoutedEventArgs e) { int ret = -1; string sql; sqliteCommand cmd = new sqliteCommand(); cmd.Connection = DBConnection; /* 删除数据 */ sql = "DELETE FROM persons"; cmd.CommandText = sql; ret = cmd.ExecuteNonQuery(); Console.WriteLine($"{ret}行被删除!"); } private void tbsqliteUpdate_Click(object sender,RoutedEventArgs e) { int ret = -1; string sql; sqliteCommand cmd = new sqliteCommand(); cmd.Connection = DBConnection; /* 更新数据 */ sql = "UPDATE persons SET age=21 WHERE id=2"; cmd.CommandText = sql; ret = cmd.ExecuteNonQuery(); Console.WriteLine($"{ret}行被修改!"); } private void tbsqliteQuery_Click(object sender,RoutedEventArgs e) { string sql; sqliteCommand cmd = new sqliteCommand(); cmd.Connection = DBConnection; /* 查询数据库 */ sql = "select * from persons"; cmd.CommandText = sql; using (sqliteDataReader reader = cmd.ExecuteReader()) { Console.WriteLine($"Depth: {reader.Depth}"); Console.WriteLine($"HasRows: {reader.HasRows}"); Console.WriteLine($"Column count: {reader.FieldCount}"); Console.WriteLine($"RecordsAffected: {reader.RecordsAffected}"); Console.WriteLine($"StepCount: {reader.StepCount}"); Console.WriteLine($"VisibleFieldCount: {reader.VisibleFieldCount}"); Console.WriteLine($"*************************************************"); while (reader.Read()) { Console.WriteLine($"ID: {reader.GetInt32(0)} {Environment.NewLine}" + $"Name: {reader.GetString(1)} {Environment.NewLine}" + $"Age: {reader.GetInt32(2)} {Environment.NewLine}" + $"Address: {reader.GetString(3)} {Environment.NewLine}"); long size = reader.GetStream(4).Length;// 获取第4列长度 byte[] d = new byte[size]; long len = reader.GetBytes(4,d,(int)size); Console.WriteLine(StringHelper.Bytes2HexString(d)); Console.WriteLine($"======================================================================="); } } } private void tbsqliteClose_Click(object sender,RoutedEventArgs e) { DBConnection?.Close(); } } }
byte数组转十六进制字符串
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hellosqlite { public class StringHelper { public static string Bytes2HexString(byte[] bytes) { string hexString = string.Empty; if (null != bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { sb.Append(bytes[i].ToString("X2") + " "); } hexString = sb.ToString(); } return hexString; } } }原文链接:https://www.f2er.com/sqlite/198423.html