安装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();//最后关闭数据库连接
输出结果图
网友评论