1. 程式人生 > 實用技巧 >WPF sqlite 資料庫入門

WPF sqlite 資料庫入門

nuget 包,安裝 System.Data.SQLite.Core

引用 using System.Data.SQLite;

可以安裝SQLite Expert 軟體

方法一

#region sqlite3
                
                string dbPath = System.IO.Path.Combine(Environment.CurrentDirectory, "zk20200828.db");
                string connString = string.Format("Data Source={0}", dbPath);

                using (SQLiteConnection conn = new SQLiteConnection(connString))
                {
                    StringBuilder query = new StringBuilder();
                    query.Append("SELECT * ");
                    query.Append("FROM Teacher1 ");

                    using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
                    {
                        conn.Open();

                        using (SQLiteDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                Console.WriteLine("SQLiteDataReader {0} {1} {2}",
                                    dr.GetValue(0),
                                    dr.GetValue(1),
                                    dr.GetValue(2));
                            }
                        }
                    }
                }
                
                #endregion

  

方法二

                #region sqlite 2
                
                string dbConnectionString = @"Data Source=E:\Code\ACCESS_Pictrue\CraftTheWorldPcSqLite\bin\Debug\zk20200828.db;Version=3;";
                try
                {
                    SQLiteConnection sqlite_con = new SQLiteConnection(dbConnectionString);
                    sqlite_con.Open();
                    string query = "select * from Teacher1;";
                    SQLiteCommand sqlite_cmd = new SQLiteCommand(query, sqlite_con);
                    SQLiteDataReader dr = sqlite_cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Console.WriteLine("SQLiteDataReader{0}",dr.GetString(1));
                     //   MessageBox.Show(dr.GetString(1));
                    }

                    sqlite_con.Close();
                }
                catch (Exception ex)
                {
                //    MessageBox.Show(ex.ToString());
                    Console.WriteLine("SQLiteDataReader{0}",ex.ToString());
                }
                
                #endregion