WCF學習之旅—第三個示例之三(二十九)
在此步驟中,將在應用程序中添加書籍的保存功能,涉及兩個功能“新增”與“修改”。
private void btnSave_Click(object sender, EventArgs e)
{
try
{
using (ChannelFactory<IBookService> channelFactory = new ChannelFactory<IBookService>("WSHttpBinding_IBookService"))
{
IBookService proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
if (string.IsNullOrEmpty(txtBookId.Text))
{
textBoxMsg.Text = proxy.Add(GetBookInfo());
}
else
textBoxMsg.Text = proxy.Edit(GetBookInfo());
}
}
}
catch (FaultException<SQLError> fex)
{
SQLError error = fex.Detail;
textBoxMsg.Text = string.Format("拋出一個服務端錯誤。\r\n\t錯誤代碼:{0}\n\t錯誤原因:{1}\r\n\t操作:{2}\r\n\t錯誤信息:{3}\r\n\r\n",
fex.Code, fex.Reason, error.Operation, error.ErrorMessage);
}
}
4. 在 buttonClear_Click事件處理程序中添加以下代碼
private void buttonClear_Click(object sender, EventArgs e)
{
txtBookId.Text = string.Empty;
txtAuthorID.Text = string.Empty;
textBoxName.Text = string.Empty;
textBoxCategory.Text = string.Empty;
textBoxPrice.Text = string.Empty;
textBoxRating.Text = string.Empty;
textBoxNumberofcopies.Text = string.Empty;
txtPublishDate.Text = string.Empty;
}
5.在菜單欄上,依次選擇“調試”和“啟動調試”以運行應用程序。
6. 在界面中使用鼠標點擊“查詢”按鈕,顯示出全部的書籍記錄。
7. 使用鼠標選中BookID=6的書籍信息,然後點擊“查詢選中的書籍”,這時會在“詳細信息”中顯示這本書的詳細信息。
8. 分別修改“價格”與“出版日期”,然後使用鼠標點擊“保存”按鈕。