ASP.NET 連接 SQLite 範例

            //建立資料表
            SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
            // Open
            conn.Open();
            //要下任何命令先取得該連結的執行命令物件
            SQLiteCommand cmd = conn.CreateCommand();
            // 建立資料表
            string sql = "CREATE TABLE IF NOT EXISTS 'address_book' (sid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , name VARCHAR, phone VARCHAR, address VARCHAR,creation_date DATETIME DEFAULT CURRENT_TIMESTAMP)";
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();

            cmd.CommandText = "INSERT INTO 'address_book' ('name', 'phone', 'address' ) VALUES('" + TextBox1.Text +"', '" + TextBox2.Text + "', '" + TextBox3.Text + "')";
            cmd.ExecuteNonQuery();

            // 查詢資料表
            cmd.CommandText = "SELECT * FROM address_book";
            // 執行查詢塞入 sqlite_datareader
            SQLiteDataReader sqlite_datareader = cmd.ExecuteReader();
            GridView1.DataSource = sqlite_datareader;
            GridView1.DataBind();

            conn.Close();

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *