当前位置: 首页 > 工具软件 > c2sqlite > 使用案例 >

c#创建数据库连接对象 sqlite数据库

唐海阳
2023-12-01

参考文章来源SQLite 之 C#版 System.Data.SQLite 使用

class db {
    private SQLiteConnection dbConnection;
    //private SQLiteCommand cd;
    //private SQLiteDataReader read;

    public SQLiteConnection lian(string path)
    {
        //创建连接字符串
        try
        {
            string databaseFileName = path;
            string connectionString = "data source = " + databaseFileName;
            //链接数据库
             dbConnection = new SQLiteConnection(connectionString);
            dbConnection.Open();
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
        return dbConnection;
    }
        
}


namespace qq
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            db b= new db();
             //创建连接字符串
            string path="C:/QQ.db";
            
            //string databaseFileName = "C:/QQ.db";
            //string connectionString = "data source = " + databaseFileName;
            //链接数据库
            //SQLiteConnection dbConnection = new SQLiteConnection(connectionString);
            //打开数据库
            //dbConnection.Open();
            //sql语句
            string sql = "select name from sqlite_master where type='table' order by name;";
            //加载sql
            SQLiteCommand cd = new SQLiteCommand(sql, b.lian(path));
            //执行
            SQLiteDataReader dr = cd.ExecuteReader();
            while (dr.Read())//读取
            {
                //Console.Write(dr["name"]+"\n");
                string str = dr["name"].ToString();
                string pan = str.Substring(0, 6);
                //Console.Write(pan+"\n");
                if (pan == "tb_Tro"||pan=="tb_c2c") {
                    Console.Write(pan + "\n");
                    
                }
            }

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //创建连接字符串
            string databaseFileName = "C:/QQ.db";
            string connectionString = "data source = " + databaseFileName;
            //链接数据库
            SQLiteConnection dbConnection = new SQLiteConnection(connectionString);
            //打开数据库
            dbConnection.Open();
            //sql语句
            string sql = "select * from tb_c2cMsg_1030331506";
            //加载sql
            SQLiteCommand cd = new SQLiteCommand(sql, dbConnection);
            //执行
            SQLiteDataReader dr = cd.ExecuteReader();
            while (dr.Read())//读取
            {
                DateTime d = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//将时间戳转年月日
                long t = long.Parse(dr["time"] + "0000000");
                TimeSpan to = new TimeSpan(t);
                Console.Write(d.Add(to) + "\n");
                var time6 = d.Add(to);
                ListViewItem lt = new ListViewItem();
                //将数据库数据转变成ListView类型的一行数据
                lt.Text = dr["uin"].ToString();
                lt.SubItems.Add(time6.ToString());
                //lt.SubItems.Add(dr["pwd"].ToString());
                //将lt数据添加到listView1控件中
                listView1.Items.Add(lt);
                int panduan = dr.GetInt32(4);
                //Console.Write(panduan);
                if (panduan == 1)
                {
                    Console.Write("对方");
                }
                else
                {
                    Console.Write("我");
                }


                //var tempStr =dr["time"];
                var p = dr["content"];
                //获取纯文本用getstring   获取序号用getordinal  获取数字用getint32
                Console.Write(p + "\n");
            }
            /* cnn.Open();

             string sql = "select * from tb_c2cMsg_1030331506";
             SQLiteCommand cmd = cnn.CreateCommand();
             cmd.CommandText = sql;
             SQLiteDataReader reader = cmd.ExecuteReader();
             //List<ImportInfo> infoList = new List<ImportInfo>();
             while (reader.Read())
             {
               //  ImportInfo info = new ImportInfo();
                 var tempStr = reader.GetString(4);
                 Console.Write(tempStr);
                 //dynamic jsonInfos = JsonConvert.DeserializeObject(tempStr);
                 //var episode_title = jsonInfos.episode_title.ToString();
                 //var audio_file_name = jsonInfos.audio_file_name.ToString();
                 //var lrc_file_name = jsonInfos.lrc_file_name.ToString();
                 //infoList.Add(new ImportInfo() { data1 = data1, data2 = data2 });
             }
             */
        }
    }
}

 类似资料: