If you are not familiar with clientadataset, it is worth studying. In this example, A. Net-based WebService is established and used in Delphi.
First, a WebService is developed. I use the C # statement under vs2008 to create an ASP. NET web service project. The generation is as follows:
[Webmethod]
Public String getdata (){
String connstr = "provider = sqloledb.1; Password =/"/"; persist Security info = true; user id = sa; initial catalog = northwind; Data Source = 192.168.1.100 ";
Oledbconnection myconnection = new oledbconnection (connstr );
String strsql = "select * from employees ";
Oledbdataadapter myadapter = new oledbdataadapter (strsql, myconnection );
Dataset DS = new dataset ();
Myadapter. missingschemaaction = missingschemaaction. addwithkey;
Myadapter. Fill (DS, "employees ");
Return csds2dds. toclientdataset (DS, "employees ");
}
Csds2dds is the response parameter of csharpdatasettodelphidataset. It contains a response method that is used to convert the. NET dataset to the XML used by clientdataset. The generation of this type is as follows:
Public class csds2dds {
Public csds2dds (){
}
Public static string toclientdataset (Dataset ds, string tablename ){
Int fieldcount = Ds. Tables [tablename]. Columns. count;
String result = "<? XML version =/"1.0/" standalone =/"Yes/"?> ";
Result + = "<datapacket version =/" 2.0/"> ";
Result + = "<metadata> <fields> ";
For (INT I = 0; I <fieldcount; I ++ ){
Datacolumn Dc = Ds. Tables [tablename]. Columns [I];
String fieldtype = Dc. datatype. Name. tolower ();
// Integer
If (fieldtype. tolower () = "int32 ")
Fieldtype = "I4 ";
// Int64
If (fieldtype. tolower () = "int64 ")
Fieldtype = "i8 ";
// Float
If (fieldtype. tolower () = "single ")
Fieldtype = "R8 ";
// Memo
If (fieldtype. tolower () = "string" & DC. maxlength> 255)
Fieldtype = "bin. Hex ";
// Bytes
If (fieldtype. tolower () = "byte []")
Fieldtype = "bin. Hex ";
Result + = string. Format ("<field attrname =/" {0}/"fieldtype =/" {1}/"", DC. columnname, fieldtype );
If (fieldtype = "bin. Hex" & DC. datatype. Name. tolower () = "string ")
Result + = "subtype =/" text /"";
Else if (fieldtype = "string ")
Result + = string. Format ("width =/" {0}/"", DC. maxlength );
Else if (fieldtype = "sqldatetime ")
Result + = "subtype =/" formatted /"";
Else if (fieldtype = "bin. Hex" & DC. datatype. Name. tolower ()! = "String" | DC. datatype. Name. tolower () = "byte []")
Result + = "subtype =/" binary /"";
Result + = "/> ";
}
Result + = "</fields> <Params/> </metadata> ";
Result + = "<rowdata> ";
Int rowcount = Ds. Tables [tablename]. Rows. count;
For (INT I = 0; I <rowcount; I ++ ){
Result + = "<row rowstate =/" 4 /"";
For (Int J = 0; j <fieldcount; j ++ ){
Datatable dt = Ds. Tables [tablename];
String value = DT. Rows [I]. itemarray [J]. tostring ();
Value = value. Replace ("/" "," & quot ;");
Result + = string. Format ("{0} =/" {1 }/"",
DT. Columns [J]. columnname,
Value );
}
Result + = "/> ";
}
Result + = "</rowdata> </datapacket> ";
Return result;
}
}
This type is very clear, that is, the number of. NET data sets is generated in a cyclical manner and combined into the XML used by clientdataset.
After this step, you can deploy the WebService to IIS, and then use Delphi to directly use it. Use WSDL importer or ro to introduce the WSDL. Then you can use it after generating a single dollar.
Procedure tform1.formcreate (Sender: tobject );
VaR
XML: string;
Begin
XML: = service. getservicesoap. getdata;
Clientdataset1.xmldata: = xml;
Clientdataset1.open;
End;
After the program completes the line, it can be seen that the data volume is transferred from the. NET end to the Delphi end.
Due to the direct acquisition of the. NET data set by the Representative, there is no effect on the Delphi end.
If. net, you only need to directly modify the zookeeper on the server ,.. NET data sets are less likely to change their formats. At least from. net1.1 to the current. net3.5, it has not changed.
Complete program source region: Click Next Region
Note: iis6.0 should be used when deploying WebService, and Windows2003 should be used for website usage. If XP is used, unknown problems may occur.