当前位置:Gxlcms > 数据库问题 > C#中使用SQLite

C#中使用SQLite

时间:2021-07-01 10:21:17 帮助过:29人阅读

string tableName, string[] columns, string[] values, SQLiteType[] types, string conditions = null) { string cmd = "UPDATE " + tableName + " SET "; for (int i = 0; i < columns.Length; i++) { cmd += columns[i] + " = @" + columns[i]; if (i != columns.Length - 1) { cmd += ", "; } } if (!string.IsNullOrEmpty(conditions)) { cmd += " WHERE " + conditions; } List<SQLiteParameter> parameters = new List<SQLiteParameter>(); for (int i = 0; i < types.Length; i++) { SQLiteParameter sp = new SQLiteParameter(); sp.ParameterName = "@" + columns[i]; sp.Value = values[i]; switch (types[i]) { case SQLiteType.INTEGER: sp.DbType = DbType.Int64; break; case SQLiteType.REAL: sp.DbType = DbType.Double; break; case SQLiteType.BLOB: sp.DbType = DbType.Binary; break; default: sp.DbType = DbType.String; break; } parameters.Add(sp); } return Execute(cmd, parameters.ToArray()); } /// <summary> /// 删除数据 /// </summary> /// <param name="tableName">表名</param> /// <param name="conditions">删除数据时的过滤条件</param> /// <returns>被删除的行数</returns> public int DeleteData(string tableName, string conditions = null) { string cmd = "DELETE FROM " + tableName; if (!string.IsNullOrEmpty(conditions)) { cmd += " WHERE " + conditions; } return Execute(cmd); } } }

 

可通过下面的样例程序调用,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLiteDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string database = "test.db";
            string tablename = "TABLE1";
            string[] columns = new string[] { "ID", "NAME", "SALARY"};
            SQLiteType[] types = new SQLiteType[] { SQLiteType.INTEGER, SQLiteType.TEXT, SQLiteType.REAL };
            string[] infos = new string[] { "PRIMARY KEY", "NOT NULL", "" };

            SQLiteHelper helper = new SQLiteHelper(database);
            helper.CreateTable(tablename, columns, types, infos);

            Random r = new Random();
            for (int i = 0; i < 5; i++)
            {
                string[] values = new string[] { i.ToString(), Guid.NewGuid().ToString(), (r.NextDouble() * 10000).ToString() };
                helper.InsertData(tablename, types, values);
            }

            System.Data.DataSet ds = helper.Select(tablename, null, null);
            System.Data.DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Console.Write(columns[j] + " : " + dt.Rows[i][j].ToString());
                    if (j != dt.Columns.Count - 1)
                    {
                        Console.Write(", ");
                    }
                    else
                    {
                        Console.Write("\n");
                    }
                }
            }
            Console.WriteLine();

            string[] newValues = new string[] { "10", "‘James‘", "12345.67" };
            string condition1 = "ID = 3";
            helper.UpdateData(tablename, columns, newValues, condition1);
            string condition2 = "ID = 2 OR ID = 0";
            helper.DeleteData(tablename, condition2);
            newValues = new string[] { "123", "Clark", "7654.32" };
            string condition3 = "ID = 1";
            helper.UpdateBinary(tablename, columns, newValues, types, condition3);

            System.Data.SQLite.SQLiteDataReader reader = helper.Select("SELECT * FROM " + tablename);
            while(reader.Read())
            {
                for (int i = 0; i < columns.Length; i++ )
                {
                    Console.Write(columns[i] + " : " + reader[columns[i]]);
                    if (i != columns.Length-1)
                    {
                        Console.Write(", ");
                    }
                    else
                    {
                        Console.Write("\n");
                    }
                }
                
            }

            helper.DropTable(tablename);
            Console.Read();
        }
    }
}

运行结果如下。

技术图片

 

整个VS工程可在下面的网址下载

https://download.csdn.net/download/u014559935/12528241

推荐:站长

C#中使用SQLite

标签:man   while   select   增删改查   nec   down   类型   values   console   

人气教程排行