WPF 自动选择dll,以SQLite为例

在学习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";
 7             string dll64 = @".SQLitedllSystem.Data.SQLite64.dll";
 8             string dllpath = @".System.Data.SQLite.dll";
 9 
10             if (IntPtr.Size == 8)
11             {
12                 using (FileStream fs=File.Create(dllpath)){}
13                 File.Copy(dll64,dllpath,true);
14             }
15             else if(IntPtr.Size == 4)
16             {
17                 using (FileStream fs=File.Create(dllpath)){}
18                 File.Copy(dll32,dllpath,true);
19             }
20             else
21             {MessageBox.Show("ERROR!");}
22             //start up the main window
23             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         {
 3             string strconn = @"Data Source=.student.db;Version=3";
 4             string strcmd = "select * from stu";
 5             SQLiteConnection con = new SQLiteConnection(strconn);
 6             try
 7             {
 8                 con.Open();
 9             }
10             catch (Exception ex)
11             { MessageBox.Show(ex.ToString()); }
12 
13 
14             SQLiteCommand cmd = new SQLiteCommand(strcmd, con);
15 
16 
17             cmd.ExecuteNonQuery();
18 
19             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来查看一下]

原文地址:https://www.cnblogs.com/wbbice/p/3963366.html