cmdInsert.Parameters.Add("@guest_name", OleDbType.VarWChar, 255, "guest_name");
//…
OleDbCommand cmdUpdate = new OleDbCommand();
cmdUpdate.Connection = conn;
cmdUpdate.CommandText = "Update t_gbook_postinfo Set last_reply_time=@last_reply_time,guest_name=@guest_name,guest_email=@guest_email,guest_website_name=@guest_website_name,guest_website_url=@guest_website_url,guest_oicq=@guest_oicq,guest_ip=@guest_ip,guest_post_time=@guest_post_time,guest_text=@guest_text,reply_data=@reply_data Where (id=@Original_id)";
cmdUpdate.Parameters.Add("@last_reply_time", OleDbType.DBDate, 0, "last_reply_time");
cmdUpdate.Parameters.Add("@guest_name", OleDbType.VarWChar, 255, "guest_name");
//…
OleDbCommand cmdDelete = new OleDbCommand();
cmdDelete.Connection = conn;
cmdDelete.CommandText = @"Delete From t_gbook_postinfo where (id = @Original_id)";
cmdDelete.Parameters.Add("@Original_id", OleDbType.Integer, 0, "id").SourceVersion = DataRowVersion.Original;
OleDbDataAdapter ada = new OleDbDataAdapter(cmdSelect);
ada.InsertCommand = cmdInsert;
ada.UpdateCommand = cmdUpdate;
ada.DeleteCommand = cmdDelete;
ada.TableMappings.Add(CMessageData.TableMapping);
return ada.Update(messageData, "t_gbook_postinfo");
}
把数据访问层单独提取出来的好处就是其他层都不会直接和数据库打交道,如果我们把数据库从Access改成SqlServer只需要用一个新的CDataAccess类替换现在的即可。在源码中,就有一个使用了Odbc.Net实现的COdbcDataAccess,用这个替换掉CDataAccess不会对程序中其他部分产生任何影响,我们可以利用Odbc.Net的访问能力,把数据库改为Oracle、Forpro等。
三