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

数据显示.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<ManagerInfo> list = new List<ManagerInfo>();
            //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;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,429评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,733评论 19 139
  • 你走,窗黯淡了月光 你走,鹊喑哑了旋律 你走,云淹没了星星的闪烁 你走,大海吞没了海鸥 你走,花蕾枯萎了葳蕤 你走...
    daladel阅读 197评论 0 0
  • 今夜,这漫漫高考长路终究是告一段落了,很不幸提前知道了录取消息;曾经说一切都是最好的安排,其实到头来还是有些可惜和...
    独立日的折耳猫阅读 749评论 21 10
  • 九十春光一掷梭,花前酌酒唱高歌; 枝上花开能几日?世上人生能几何? 昨朝花胜今朝好,今朝花落成秋草; 花前人是去年...
    梅园遗珠阅读 1,816评论 1 2

友情链接更多精彩内容