天天看點

一起談.NET技術,.NET遠端處理架構詳解

  第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. }

伺服器端代碼:

1. privatevoidfrmSupplierServer_Load(objectsender,System.EventArgse)

2. {TcpChannelchan=newTcpChannel(8888);

3. ChannelServices.RegisterChannel(chan);

4. //注冊提供服務的遠端對象

5. RemotingConfiguration.RegisterWellKnownServiceType

     (typeof(DbServerLibrary.DbServer)

"DbServer",WellKnownObjectMode.Singleton);

6. }

用戶端代碼:

解析SQL:SqlParse.cs

1. namespaceSupplierClient{

2. publicclassSqlParse{

3. //得到sql語句的類型

4. publicstringGetSqlType(stringsqlText)//typeofSQLstatements{

5. }

6. //得到select語句要查詢的表名

7. publicstringGetSelectTableName(stringsqlText){

8. }

9. //得到select語句中的where子句

10. publicstringGetWhereClause(stringsqlText){

11. }

12. //得到查詢條件中的字段名

13. publicstringGetSelectField(stringsqlText){

14. }

15. //得到分片依據,傳回Scity的值

16. publicstringGetSelectCityValue(stringsqlText){

18. //設定select語句經解析後的格式

19. publicArrayListSetSelectList(stringsqlText){

21. //如果沒有分片資訊,則向3個場地都發送指令

22. publicArrayListSendToAllSite(stringsqlText){

23. }

24. //得到insert語句要查詢的表名

25. publicstringGetInsertTableName(stringsqlText){

26. }

27. //根據插入的表和值,設定場地:INSERTINTOSupplierVALUES('no','name','city'),returncity

28. publicstringGetInsertCityValue(stringsqlText){

29. }

30. //如果表名是Supplier,則根據city值設定向哪個場地發送指令

31. publicArrayListSetInsertSite(stringsqlText){

32. }

33. //生成解析後的insert指令清單

34. publicArrayListSetInsertList(stringsqlText){

35. }

36. namespaceSupplierClient{

37. publicclassLocalServer{

38. }

39. //傳回查詢結果

40. publicDataSetMakeDataSet(stringsqlText){

41. }

42. //執行插入和删除操作,并傳回影響記錄數

43. publicintExecuteSql(stringsqlText){

44. }

  第4 章界面

  4.1 用戶端

  用戶端啟動後,使用者首先在文本框中輸入SQL指令,然後通過解析後向相應場地發送指令,并将傳回的結果集進行合并,顯示在界面中,顯示結果後空白的文本框用來顯示執行插入删除操作時的結果資訊。

  4.2 伺服器

  伺服器端僅實作對遠端對象的注冊,是以界面不需要實作功能,隻需要在啟動時注冊遠端對象即可,接收到的用戶端的使用者指令是通過消息框顯示的。如上圖所示。

  第5 章指令處理及核心算法流程

  Insert 操作——

1. //得到insert語句要查詢的表名

2. publicstringGetInsertTableName(stringsqlText){

3. }

4. //根據插入的表和值,設定場地:INSERTINTOSupplierVALUES('no','name','city'),returncity

5. publicstringGetInsertCityValue(stringsqlText){

7. //如果表名是Supplier,則根據city值設定向哪個場地發送指令

8. publicArrayListSetInsertSite(stringsqlText){

9. }

10. //生成解析後的insert指令清單

11. publicArrayListSetInsertList(stringsqlText){

12. }

  Delete 操作——

  向各個場地發送,通過定義資料庫中表的關系及限制來保證完整性和一緻性,如果删除指令不成功,則傳回異常資訊,否則,傳回各個場地成功執行指令影響的記錄數目。

  Select 操作——

1. //得到sql語句的類型

2. publicstringGetSqlType(stringsqlText)//typeofSQLstatements{

4. //得到select語句要查詢的表名

5. publicstringGetSelectTableName(stringsqlText){

7. //得到select語句中的where子句

8. publicstringGetWhereClause(stringsqlText){

10. //得到查詢條件中的字段名

11. publicstringGetSelectField(stringsqlText){

13. //得到分片依據,傳回Scity的值

14. publicstringGetSelectCityValue(stringsqlText){

15. }

16. //設定select語句經解析後的格式

17. publicArrayListSetSelectList(stringsqlText){

18. }

19. //如果沒有分片資訊,則向3個場地都發送指令

20. publicArrayListSendToAllSite(stringsqlText){

21. }

  第6章結論

  .NET遠端處理架構提供的一項強大的技術,利用它可以使位于任何位置的應用程式互相通信,這些應用程式可能在同一台計算機上運作,也可能位于同一區域網路中的不同計算機上,或者位于相隔萬裡的有巨大差異的網絡中。

  使用.NET Remoting技術結合ADO.Net能夠高效、可靠地解決這兩方面的問題。具體表現為,在C#中通過使用.Net遠端處理架構能夠友善地解決資料、指令遠端傳遞問題;C#通過ADO.Net對資料庫進行操作,使分布式資料庫系統中對資料庫的各種操作變得高效、可靠,同時易于解決資料一緻性問題。

  由于時間關系,程式中仍有部分bug,将在下一步繼續完善,而且,還應進一步完善資料字典,使程式結構更加清晰,增強可擴充性。