|
第1章系統總體結構
1.1 總體結構
系統實現需要部署服務器端的遠程對象(即一個DbServerLibrary.dll),服務器端要注冊通道和該遠程對象。客戶端要實現一個本地查詢的服務器,同時根據SQL解析的結果向各個服務器發送命令,并將結果顯示在客戶端界面,服務器端可以接受并顯示相應的命令。
1.2 關鍵組件結構
系統結構中關鍵的組件有遠程對象,和本地服務器,實現的功能基本一致。下面以遠程對象為例,說明組件的實現。遠程對象在服務器端解決方案下的庫文件中聲明,通過服務器端進行注冊,客戶端通過TCP通道與服務器端遠程對象通信,實現數據集的查詢和傳輸。主要的數據成員有:SqlConnection(SQL Server數據庫的連接對象)、 SqlCommand (SQL命令對象)、SqlDataAdapter(數據適配器,填充數據集)組件——DbServerLibrary。
第2 章.NET遠程處理框架提供的強大技術
因時間倉促,未實現數據字典,所有實驗要求的SQL經過解析后,直接通過代碼判斷,向相應場地發送命令。
代碼分為三部分:遠程對象,服務器端代碼和客戶端代碼。
其中:遠程對象部署在各個服務器端,客戶端除了實現查詢命令的解析和傳送外外,還有一個本地服務器,進行相應的本地查詢。
遠程對象代碼:
1. usingSystem;
2. usingSystem.Runtime.Serialization;
3. usingSystem.Data;
4. usingSystem.Data.SqlClient;
5. usingSystem.Windows.Forms;
6. namespaceDbServerLibrary{
7. [SerializableAttribute]//ItisveryimportantforRemotingData
8. publicclassDbServer:MarshalByRefObject{
9. privatestringconnStr;
10. privatestringclientSql;
11. publicSqlConnectionsqlConn;
12. publicSqlCommandsqlComm;
13. publicSqlDataAdaptersqlAdapter;
14. publicvoidGetClientSql(stringsql){
15. if(clientSql!=null){
16. clientSql=null;
17. }
18. clientSql=sql;
19. MessageBox.Show(clientSql);
20. }
21. publicDbServer(){
22. //LocalDataInitialize
23. cnnStr="DataSource=localhost;InitialCatalog=DDB;UserID=sa;Password=;";
24. sqlConn=newSqlConnection(connStr);
25. }
26. publicDataSetGetDataSet()
27. //執行select
28. DataSetds=newDataSet();
29. if(sqlComm!=null){
30. sqlComm=null;
31. }
32. if(sqlConn.State==ConnectionState.Closed){
33. sqlConn.Open();
34. }
35. try{
36. sqlComm=newSqlCommand();
37. sqlComm.Connection=sqlConn;
38. sqlComm.CommandText=clientSql;
39. sqlComm.CommandType=CommandType.Text;
40. sqlAdapter=newSqlDataAdapter();
41. sqlAdapter.SelectCommand=sqlComm;
42. sqlAdapter.Fill(ds);
43. }
44. catch(SqlExceptionex){
45. MessageBox.Show(ex.Message);
46. }
47. returnds;
48. }
49. publicintExecuteSql()//執行insert和delete{
50. intaffectedNumber;
51. if(sqlComm!=null){
52. sqlComm=null;
53. }
54. if(sqlConn.State==ConnectionState.Closed){
55. sqlConn.Open();
56. }
57. try{
58. sqlComm=newSqlCommand();
59. sqlComm.Connection=sqlConn;
60. sqlComm.CommandType=CommandType.Text;
61. sqlComm.CommandText=clientSql;
62. affectedNumber=sqlComm.ExecuteNonQuery();
63. returnaffectedNumber;
64. }
65. catch(SqlExceptionex){
66. MessageBox.Show(ex.Message);
67. return0;
68. }
69. }
70. }
71. }
NET技術:.NET遠程處理框架詳解,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。