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();
}
資料庫