C#通过Ado.NET操作SQLite数据库

C#通过Ado.NET操作SQLite数据库_第1张图片
数据显示.jpg

记得引入程序集System.Data.SQLite

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//命名空间
using System.Data.SQLite;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            List list = new List();
            //1创建SQLite数据库连接字符串
            string conStr = @"data source=D:\项目工程文件夹\尚云网络项目开发\尚云网络\SQLiteData\Sudons.db;version=3";
            //2创建SQLite数据库连接对象
            using (SQLiteConnection con = new SQLiteConnection(conStr))
            {
                //3打开数据库
                con.Open();
                //4构建SQL语句
                string cmdStr = "select * from ManagerInfo";
                using (SQLiteCommand cmd = new SQLiteCommand(cmdStr,con))
                {
                    //5执行SQL命令
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        //6读取数据
                        while (reader.Read())
                        {
                            list.Add(new ManagerInfo()
                            {
                                Mid = Convert.ToInt32(reader["MId"]),
                                Mname = reader["MName"].ToString(),
                                Mpwd = reader["MPwd"].ToString(),
                                Mtype = Convert.ToInt32(reader["MType"])
                            });
                        }//end while
                    }//end if
                }//end using cmd
            }//end using con

            //7给DataGridView绑定数据源
            this.dataGridViewManagerInfo.DataSource = list;
        }
    }
}

你可能感兴趣的:(C#通过Ado.NET操作SQLite数据库)