在学习sqlite的过程中,发现它的dll是区分32位和64位的,起初觉得很恼火,但是仔细看了下,
发现让程序自行选择dll其实也不是一件很麻烦的事情,如下:
1>创建一个sqlite数据
2>创建一个工程
3>新建一个类
1
class
Entrance : Application
2
{
3
[STAThread]
4
static
void
Main()
5
{
6
string dll32 = @".SQLitedllSystem.Data.SQLite32.dll";
7string dll64 = @".SQLitedllSystem.Data.SQLite64.dll";
8string dllpath = @".System.Data.SQLite.dll";
910if (IntPtr.Size == 8)
11 {
12using (FileStream fs=File.Create(dllpath)){}
13 File.Copy(dll64,dllpath,true);
14 }
15elseif(IntPtr.Size == 4)
16 {
17using (FileStream fs=File.Create(dllpath)){}
18 File.Copy(dll32,dllpath,true);
19 }
20else21 {MessageBox.Show("ERROR!");}
22//start up the main window23 Application app = new Application();
24 MainWindow window = new MainWindow();
25 app.Run(window);
26 }
27 }
4>添加按钮响应事件
1
private
void Button_Click(object sender, RoutedEventArgs e)
2 {
3string strconn = @"Data Source=.student.db;Version=3";
4string strcmd = "select * from stu";
5 SQLiteConnection con = new SQLiteConnection(strconn);
6try 7 {
8 con.Open();
9 }
10catch (Exception ex)
11 { MessageBox.Show(ex.ToString()); }
121314 SQLiteCommand cmd = new SQLiteCommand(strcmd, con);
151617 cmd.ExecuteNonQuery();
1819 SQLiteDataAdapter dataApp = new SQLiteDataAdapter(cmd);
20 DataTable dt = new DataTable("a");
21 dataApp.Fill(dt);
22 dataGrid1.ItemsSource = dt.DefaultView;
23 dataApp.Update(dt);
24 con.Close();
25 }
26 }
5>最终效果(左边是win8 64位测试效果,右边是xp 32位测试效果)[注:新版的System.Data.SQLite.dll可能需要msvcr100.dll的支持,在测试
的机器上如果没有这个dll会莫名地崩溃,还catch不到异常,具体可能跟版本有关,可以用depends来查看一下]
原文:http://www.cnblogs.com/wbbice/p/3963366.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!