Chui-Wen's profileChui-Wen Chiu's NoteBlogListsGuestbookMore Tools Help

Blog


    2/16/2006

    SQLite for C# 測試

    /**
     * SQLite 測試
     *
     * 環境:
     * [1] Windows 2000 PRofessional
     * [2] Finisar.SQLite 0.21, http://adodotnetsqlite.sourceforge.net/downloads/
     * [3] SQLite3, http://www.sqlite.org/download.html
     * [4] Visual Studio 2005 Express
     *
     * 設計者:Chui-Wen Chiu(Arick)
     *
     * 日誌:
     *  2006/02/16  建立
     **/
    using System;
    using System.Data;
    using Finisar.SQLite;
    using System.Text;
      
    namespace SQLiteTest {
        class Program {
            public static SQLiteCommand sqlite_cmd;
            /// <summary>
            /// 建立 DB
            /// </summary>
            public static void createDB() {
             string sql = "CREATE TABLE user("+
               "id INTEGER PRIMARY KEY, "+
               "name text, "+
               "sex int(1) NOT NULL DEFAULT 0 "+
                  ")";
                sqlite_cmd.CommandText = sql;
                sqlite_cmd.ExecuteNonQuery();
            }
            /// <summary>
            /// 刪除 DB
            /// </summary>
            public static void removeDB() {
                string sql = "DROP TABLE user";
                sqlite_cmd.CommandText = sql;
                try {
                    sqlite_cmd.ExecuteNonQuery();
                } catch (Exception ex) {
                }
            }
            /// <summary>
            /// 新增一位使用者
            /// </summary>
            /// <param name="name">使用者名稱</param>
            /// <param name="sex">性別(0=女, 1=男)</param>
            public static void addUser(string name, int sex) {
                string sql = " INSERT INTO user (name, sex) " +
                             " VALUES ('"+name+"', "+sex+") ";
                sqlite_cmd.CommandText = sql;
                sqlite_cmd.ExecuteNonQuery();  
            }
            /// <summary>
            /// 列出全部的使用者
            /// </summary>
            public static void listUser() {
                SQLiteDataReader sqlite_datareader;
              
                string sql = "SELECT * from user";
                sqlite_cmd.CommandText = sql;           
                sqlite_datareader = sqlite_cmd.ExecuteReader();
               
                Console.WriteLine("id\tname\tsex");
                Console.WriteLine("------------------------------------------------");
                while (sqlite_datareader.Read()) {
                    Console.WriteLine("{0}\t{1}\t{2}",
                        sqlite_datareader["id"],
                        sqlite_datareader["name"],
                        sqlite_datareader["sex"]);
                }
            }
            static void Main(string[] args) {
                // 建立連線
                SQLiteConnection sqlite_conn;                       
                sqlite_conn = new SQLiteConnection("Data Source=ChuiWenChiu.db;Version=3;New=True;Compress=True;");           
                sqlite_conn.Open();           
                // 建立查詢命令物件
                sqlite_cmd = sqlite_conn.CreateCommand();
                removeDB();
                createDB();
             addUser("arick", 1);
             addUser("mavis", 0);
             addUser("joss", 1);
             addUser("jack", 1);
             addUser("王小明", 1);
                listUser();
     
                // 關閉連線           
                sqlite_conn.Close();
            }
        }
    }
    執行結果:

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://chuiwenchiu.spaces.live.com/blog/cns!CA5D9227DF9E78E8!538.trak
    Weblogs that reference this entry
    • None