ASP.NET SQLite Sample 上課範例程式碼

        void showdata()
        {
        //建立資料表
        SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
        // Open
        conn.Open();
        //要下任何命令先取得該連結的執行命令物件
        SQLiteCommand cmd = conn.CreateCommand();
        // 建立資料表
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM address_book";
        // 執行查詢塞入 sqlite_datareader
        SQLiteDataReader sqlite_datareader = cmd.ExecuteReader();
        GridView1.DataSource = sqlite_datareader;
        GridView1.DataBind();
        conn.Close();
        }

        void executeSQL(string sql)
        {
        SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
        conn.Open();
        SQLiteCommand cmd = conn.CreateCommand();
        cmd.Connection = conn;
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery();
        conn.Close();
        }


        protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
        {
            //Set the edit index.
            GridView1.EditIndex = e.NewEditIndex;
            //Bind data to the GridView control.
            showdata();
        }
        protected void GridView1_OnRowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
        {
            Response.Write("cancel");
            GridView1.EditIndex = -1;
            //Bind data to the GridView control.
            showdata();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            //Update the values.
            GridViewRow row = GridView1.Rows[e.RowIndex];
            Label1.Text += ((TextBox)(row.Cells[2].Controls[0])).Text+"<br>";
            Label1.Text += ((TextBox)(row.Cells[3].Controls[0])).Text +"<br>";
            Label1.Text += ((TextBox)(row.Cells[4].Controls[0])).Text +"<br>";

            //更新的SQL語法

            string updatesql = "UPDATE address_book SET name='" + ((TextBox)(row.Cells[2].Controls[0])).Text + "', phone='" + ((TextBox)(row.Cells[3].Controls[0])).Text + "',address='" + ((TextBox)(row.Cells[4].Controls[0])).Text + "' where sid=" + ((TextBox)(row.Cells[1].Controls[0])).Text;
            executeSQL(updatesql);

            //Reset the edit index.

            GridView1.EditIndex = -1;

            //Bind data to the GridView control.
            showdata();
        }

資料庫

 

ASP.NET SQLite Sample

 //建立資料表
            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.Parameters.Add(cmd.CreateParameter());
            cmd.Parameters.Add(cmd.CreateParameter());
            cmd.Parameters.Add(cmd.CreateParameter());

            cmd.CommandText = "INSERT INTO 'address_book' ('name', 'phone', 'address' ) VALUES( ?, ?, ?)";
            cmd.Parameters[0].Value = TextBox1.Text;
            cmd.Parameters[1].Value = TextBox2.Text;
            cmd.Parameters[2].Value = 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();