• 大小: 480KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签: c#  sqlite  

资源简介

c#连接sqlite简单示例,文档中含简介及安装包下载地址

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;  

namespace ConsoleApplication1
{
    class MyCSharpSQLite
    {
        private static string connStr;

        static void Main(string[] args)
        {

            connStr = SQLiteConnectionString.GetConnectionString(@“D:\学习总结与资料\SqliteDev\develop\EasyDatabase“);
            Console.WriteLine(“SQLite Connect String is : {0}“ connStr);

            //连接测试  
            //testConnect(connStr);  

            //执行查询  
            ExecQuery(“select * from customers“);

            Console.ReadKey();
        }

        private static void testConnect(string connStr)
        {
            try
            {
                SQLiteConnection conn = new SQLiteConnection(connStr);

                conn.Open();
                Console.WriteLine(“SQLite Connect OK.“);
            }
            catch (Exception ex)
            {
                Console.WriteLine(“SQLite Connect fail: {0} “ ex.Message);
            }
        }

        private static void ExecQuery(string sqlStr)
        {
            Console.WriteLine(“>> Start to query the database....“);
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                SQLiteCommand comm = conn.CreateCommand();
                comm.CommandText = sqlStr;
                //comm.CommandType = comm.CommandText;  

                using (SQLiteDataReader reader = comm.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(reader[0].ToString());
                    }
                }
            }
        }


    }

    ///   

    /// 生成SQLite连接字符串  

    /// 
  

    public static class SQLiteConnectionString
    {

        public static string GetConnectionString(string path)
        {
            return GetConnectionString(path null);
        }

        public static string GetConnectionString(string path string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                return “Data Source=“ + path;
            }
            else
            {
                return “Data Source=“ + path + “;Password=“ + password;
            }
        }

    }  
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-06-14 20:54  sqlite\
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\
     文件        2685  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.csproj
     文件        2472  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\Program.cs
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\Properties\
     文件        1376  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\Properties\AssemblyInfo.cs
     文件         117  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\app.config
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\
     文件        6144  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
     文件         117  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.config
     文件       15872  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
     文件       21472  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe
     文件         117  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config
     文件         490  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest
     文件      798720  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\bin\Debug\System.Data.SQLite.dll
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\
     目录           0  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\
     文件         682  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt
     文件        6312  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csprojResolveAssemblyReference.cache
     文件        6144  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
     文件       15872  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb
     文件        6002  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2015-06-14 20:54  sqlite\ConsoleApplication1\ConsoleApplication1\obj\Debug\TempPE\
     文件         947  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1.sln
     文件       25600  2015-06-14 20:25  sqlite\ConsoleApplication1\ConsoleApplication1.v11.suo
     文件       58839  2015-06-14 20:54  sqlite\sqlite新手学习.docx

评论

共有 条评论