2009年8月10日 星期一

C sharp or .Net 使用sqlite : 5 delete data

require download System.Data.SQLite 及設定, 可見 C sharp or .Net 使用sqlite 設定

SQLiteCommand 的變數定義自行參考 SQLite.Net 的 Help

關於其他的SQL 語法,可以參考

在SQL 相關刪除的語法有3個

delete 刪除特定欄位在某個table

DELETE FROM employee WHERE id = 100;

truncate 刪除所有欄位在某個table

TRUNCATE TABLE employee;

但 SQLite 沒有 truncate 但可以用
DELETE FROM employee 的語法達成同樣效果

drop 刪除整個table


DROP TABLE employee;


以下例子,顯示相關SQLite語法

例一 刪除 table tbl 中 id =3的欄位


public void DeleteExampleTable(SQLiteCommand cmd)
{

try
{
/// Set Delete command
cmd.CommandText = "DELETE FROM [tbl] where id=3";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("delete OK");
}
catch
{
MessageBox.Show("delete Error");
}

}




例二 刪除 table tbl 所有的欄位達成 truncate 的效果



public void DeleteExampleTable(SQLiteCommand cmd)
{

try
{
/// Set Delete command
cmd.CommandText = "DELETE FROM [tbl]";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("delete OK");
}
catch
{
MessageBox.Show("delete Error");
}

}


例三 刪除 table tbl (drop)


public void DROPExampleTable(SQLiteCommand cmd)
{

try
{
/// Set drop table command
cmd.CommandText = "DROP TABLE [table]";
/// Execute the command, iF have not exist show error message
cmd.ExecuteNonQuery();
MessageBox.Show("Drop OK");
}
catch
{
MessageBox.Show("Drop Error");
}


}

沒有留言: