当前位置:Gxlcms > 数据库问题 > Winform连接数据库

Winform连接数据库

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

1.构造接收数据的集合 List<ManagerInfo> list = new List<ManagerInfo>(); //2.从数据库表ManageInfo查询数据 string connStr = @"data source=C:\Users\CHD\Desktop\ItcastCater.db;version = 3;"; //3.创建对象 using (SQLiteConnection conn = new SQLiteConnection(connStr)) { //4.创建Command对象 SQLiteCommand cmd = new SQLiteCommand("select * from ManagerInfo", conn); //5.打开链接 conn.Open(); //6.执行命令 SQLiteDataReader reader = cmd.ExecuteReader(); //7.读取 if (reader.HasRows) { 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"]) }); } } reader.Close(); //8.显示在DataGridView上 dataGridView1.DataSource = list; }

 

2.封装SqliteHelper

 <connectionStrings>
    <add name="itcast" connectionString="data source=C:\Users\CHD\Desktop\ItcastCater.db;version = 3;"/>
  </connectionStrings>
public static class SqliteHelper
    {
        //从配置文本中读取连接字符串
        private static string connStr = ConfigurationManager.ConnectionStrings["itcast"].ConnectionString;

        //执行命令的方法:insert,update,delete
        //params:可变参数,目的是省略了手动构造数组的过程,直接指定对象,编译器会帮助我们构造数组,并将对象加入数组中,传递过来
        public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
        {
            //创建连接对象
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                //创建命令对象
                SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                //添加参数
                cmd.Parameters.AddRange(ps);
                //打开连接
                conn.Open();
                //执行命令,并返回受影响的行数
                return cmd.ExecuteNonQuery();
            }
        }

        //获取首行首列值的方法
        public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                cmd.Parameters.AddRange(ps);

                conn.Open();
                //执行命令,获取查询结果中的首行首列的值,返回
                return cmd.ExecuteScalar();
            }
        }

        //获取结果集
        public static DataTable GetDataTable(string sql, params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                //构造适配器对象
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn);
                //构造数据表,用于接收查询结果
                DataTable dt = new DataTable();
                //添加参数
                adapter.SelectCommand.Parameters.AddRange(ps);
                //执行结果
                adapter.Fill(dt);
                //返回结果集
                return dt;
            }
        }
    }
 private void Form1_Load(object sender, EventArgs e)
        {
            //操作数据库的类
            //连接Connection -> SqliteConnection
            //命令Command
            //适配器DataAdapter
            //DataReader
            //DataSet、DataTable
            dataGridView1.DataSource = SqliteHelper.GetDataTable("select * from ManagerInfo");

        }

 

Winform连接数据库

标签:data   public   适配   info   ase   ddr   传递   ada   let   

人气教程排行