SQLite 与 DataGridView


如何在DataGridView中显示Sqlite的结果。


SQLite 与 DataGridView_第1张图片

需    using System.Data.SQLite;

            //数据库文件路径
            var path1 = "d:\\temp\\temp.db";
            System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
            connstr.DataSource = path1;

            //建立数据库连接
            System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
            conn.ConnectionString = connstr.ToString();
            conn.Open(); 

            //查询语句
            string sqlCommandString = "select * from Friends";
            //利用 Adapter 转换结果到 datagrid
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlCommandString, conn);  
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            DataView dv = ds.Tables[0].DefaultView;

            //关闭连接,并把结果显示在datagrid上
            conn.Close();
            conn.Dispose();
            dataGridView1.DataSource = dv;


你可能感兴趣的:(C#,SQLite)