static void cmdUpdate(string strPhone2, string strEmail2, string strRegion2, string strId2)
{
try
{
string connString = null;
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= D:\VS2005\練習" +
@"\TestDB.mdb";
using (OleDbConnection conn =
new OleDbConnection(connString))
{
conn.Open();
using (OleDbCommand cmd =
new OleDbCommand("UPDATE Infor SET Phone=@Phone, Email=@Email, Region=@Region" +
" WHERE Id=@Id", conn))
{
cmd.Parameters.AddWithValue("@Phone", strPhone2);
cmd.Parameters.AddWithValue("@Email", strEmail2);
cmd.Parameters.AddWithValue("@Region", strRegion2);
cmd.Parameters.AddWithValue("@Id", strId2);
int rows = cmd.ExecuteNonQuery();
//rows number of record got updated
cmd.Dispose();
}
}
}
catch (Exception ex)
{
//Log exception
//Display Error message
MessageBox.Show(ex.Message, "例外訊息");
}
}
//Insert
static void cmdInsert(string Id, string DepNo, string Name, string Phone, string Email, string Region, string Birth)
{
try
{
string connString = null;
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= D:\VS2005\練習" +
@"\TestDB.mdb";
using (OleDbConnection conn =
new OleDbConnection(connString))
{
conn.Open();
using (OleDbCommand cmd =
new OleDbCommand("INSERT INTO Infor VALUES(" +
"@Id, @DepNo, @Name, @Phone, @Email, @Region, @State, @Birth)", conn))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@DepNo", DepNo);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Phone", Phone);
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@Region", Region);
cmd.Parameters.AddWithValue("@State", "No");
cmd.Parameters.AddWithValue("@Birth", Birth);
int rows = cmd.ExecuteNonQuery();
//rows number of record got inserted
cmd.Dispose();
}
}
}
catch (Exception ex)
{
//Log exception
//Display Error message
MessageBox.Show(ex.Message, "例外訊息");
}
}
//Delete
static void Delete(string selectId)
{
try
{
if (MessageBox.Show("確認刪除?", "刪除資料後不可復原", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string connString = null;
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= D:\VS2005\練習" +
@"\TestDB.mdb";
using (OleDbConnection conn =
new OleDbConnection(connString))
{
conn.Open();
using (OleDbCommand cmd =
new OleDbCommand("DELETE FROM Infor " +
"WHERE Id=@Id", conn))
{
cmd.Parameters.AddWithValue("@Id", selectId);
int rows = cmd.ExecuteNonQuery();
//rows number of record got deleted
}
}
}
}
catch (Exception ex)
{
//Log exception
//Display Error message
MessageBox.Show(ex.Message, "例外資訊");
}
}
沒有留言:
張貼留言