C#连接SQLite数据库

安装SQLite 建好表和添加数据
在App.config里添加连接字符串

<connectionStrings>
   <add name="sqlite" connectionString="Data Source=d:\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\mysqlite.db;Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite" />
  </connectionStrings>

将数据库.db文件放在解决方案的/bin/Debug/目录下面
进行一波读取USER表的信息操作

 string sql = "SELECT * FROM user"; //sql语句
            string connStr = @"Data Source=" + @"d:\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\mysqlite.db;Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";//连接字符串
            SQLiteConnection conn = new SQLiteConnection(connStr);//新建连接实例
            conn.Open();//连接数据库
            SQLiteCommand cmd = new SQLiteCommand(sql,conn);/新建操作命令实例
            SQLiteDataReader reader = cmd.ExecuteReader();//获取返回数据
            while (reader.Read()) {//循环返回的数据
                Console.WriteLine("Name:"+reader["UserName"]+"-PWD:"+reader["Password"]);
                Console.ReadLine();
            }
            conn.Close();//最后关闭数据库连接
输出结果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。