vs安装sqlite过程:
前提条件:我的当前环境:win7旗舰版、vs2010版。
一、安装sqlite。点击这里下载sqlite
1、打开安装包
点击"next"
选择安装路径,如"D:\Program Files\System.Data.SQLite\2010"
点击"next",选择"Full installation"
点击"next"
点击"next"
点击"next"
点击"Install",等待程序自动安装完成。点击"finish",完成sqlite的安装。
二、在vs中使用sqlite
创建C#窗体应用程序
拖拽控件做一个测试窗体
其中控件1的Name为dgvShow,控件2的Name为btnCreateDatabase,控件3的Name为btnCreateTable
右键点击项目中的应用,选择"添加应用"
点击确定完成引用的添加。
Form1.cs[设计]界面代码如下(注意要添加using System.Data.SQLite;):
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Data.SQLite; 10 11 namespace SqliteTest 12 { 13 public partial class Form1 : Form 14 { 15 16 private string dataSource = "TestDB.sqlite"; 17 private SQLiteConnection dbConn; 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void btnCreateDatabase_Click(object sender, EventArgs e) 25 { 26 SQLiteConnection.CreateFile(dataSource); 27 btnCreateDatabase.Text = "数据库创建完成"; 28 dbConn = new SQLiteConnection("Data Source=" + dataSource); 29 } 30 31 private void btnCreateTable_Click(object sender, EventArgs e) 32 { 33 dbConn.Open(); 34 35 SQLiteCommand dbCmd = dbConn.CreateCommand(); 36 37 dbCmd.CommandText = "CREATE TABLE tabname (id varchar(20) ,dogname varchar(30), mousename varchar(20))"; 38 39 40 dbCmd.ExecuteNonQuery(); 41 42 dbConn.Close(); 43 44 btnCreateTable.Text = "表格创建完成"; 45 } 46 47 private void btnInsert_Click(object sender, EventArgs e) 48 { 49 dbConn.Open(); 50 SQLiteCommand dbCmd = dbConn.CreateCommand(); 51 dbCmd.CommandText = "INSERT INTO tabname VALUES(‘20投‘ , ‘沙漠死神‘ ,‘图奇‘)"; 52 53 dbCmd.ExecuteNonQuery(); 54 dbConn.Close(); 55 btnInsert.Text = "插入成功"; 56 } 57 58 private void btnShow_Click(object sender, EventArgs e) 59 { 60 dbConn.Open(); 61 SQLiteCommand dbCmd = dbConn.CreateCommand(); 62 dbCmd.CommandText = "SELECT * FROM tabname"; 63 SQLiteDataReader dataReader = dbCmd.ExecuteReader(); 64 65 DataTable dataTable = new DataTable(); 66 67 if (dataReader.HasRows) 68 { 69 70 dataTable.Load(dataReader); 71 72 } 73 74 dgvShow.DataSource = dataTable; 75 76 dataReader.Close(); 77 dbCmd.Dispose(); 78 dbConn.Close(); 79 btnShow.Text = "显示成功"; 80 } 81 } 82 }
c#使用sqlite的简单操作就此完成,若要实现更复杂的功能请问“度娘”。