如何在 Delphi 与 .NET Web Service 之间互相传输DataSet (1)

.NET Web Service 中 DataSet 参数传输时,  是把 DataSet  转换成 XML 格式的串进行传输,
这样的弊端是,  数据传输过大, 还有的就别的语言写的程序很难调用

本人在一个项目过程中, 需要.NET 写 Web Service, 而客户端需要Delphi来实现
鉴于这个原因,  我通过把DataTable转换成文本串的方法,

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 把DataTable转换成文本串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dt">DataTable对象</param>
ExpandedBlockEnd.gif        
/// <returns>返回返回的文本串</returns>

None.gif          public   static   string  DataSetToString(DataTable dt)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            System.Text.StringBuilder sw 
= new System.Text.StringBuilder();
InBlock.gif
InBlock.gif            
bool bFirst = true
InBlock.gif
InBlock.gif            
foreach (System.Data.DataColumn dc in dt.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!bFirst)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sw.Append(
",");
ExpandedSubBlockEnd.gif                }

InBlock.gif                bFirst 
= false;
InBlock.gif                sw.Append(
"\"" + dc.ColumnName + "\"");            
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int intColumnCount = dt.Columns.Count;
InBlock.gif
InBlock.gif            
foreach (System.Data.DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i=0; i<intColumnCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sw.Append(
",");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sw.Append(
"\r\n");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if (dr[i] != System.DBNull.Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (dt.Columns[i].DataType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sw.Append(
"\"" + dr[i].ToString().Replace("\"""\"\""+ "\"");
ExpandedSubBlockEnd.gif
                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sw.Append(dr[i].ToString());
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sw.ToString();
ExpandedBlockEnd.gif        }

None.gif

提供一个取随机字符串的函数
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 产生随机文件名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="length">随机文件名长度</param>
ExpandedBlockEnd.gif        
/// <returns>产生随机文件名</returns>

None.gif          private   static   string  GetRandomFileName( int  length)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string ch = "abcdefghijklmnopqrstuvwxyz0123456789";
InBlock.gif
InBlock.gif            
string strRandomFileName = string.Empty;
InBlock.gif
InBlock.gif            
int intLength = ch.Length;
InBlock.gif
InBlock.gif            Random rd 
= new Random();
InBlock.gif
InBlock.gif            
for (int i=0; i<length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                strRandomFileName 
+= ch[rd.Next(intLength)];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strRandomFileName;
ExpandedBlockEnd.gif        }


下面的方法是在.NET中如何把文本串转换成DataTable的方法, 此方法同样适合在其它语言中使用
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 把文件串转化成DataTable
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strDataSet">DTS文本串</param>
ExpandedBlockEnd.gif        
/// <returns>返回的DataTable</returns>

None.gif          public   static  DataTable StringToDataSet( string  strDataSet)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strTempFileName = string.Empty;
InBlock.gif            
string strTempFullName = string.Empty;
InBlock.gif            
string strPath = System.IO.Path.GetTempPath();
InBlock.gif
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTempFileName 
= GetRandomFileName(15);
InBlock.gif                strTempFullName 
= strPath + strTempFileName + ".txt";
InBlock.gif                
if (!System.IO.File.Exists(strTempFullName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            System.IO.StreamWriter sw 
= new System.IO.StreamWriter(strTempFullName, false, System.Text.Encoding.Default);
InBlock.gif            sw.Write(strDataSet);
InBlock.gif            sw.Close();        
InBlock.gif    
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
InBlock.gif                
+ "Extended Properties=Text;Persist Security Info=False", strPath));
InBlock.gif            conn.Open();
InBlock.gif
InBlock.gif            OleDbDataAdapter cmd 
= new OleDbDataAdapter(string.Format("select * from {0}#txt", strTempFileName), conn);
InBlock.gif
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            cmd.Fill(ds);
InBlock.gif
InBlock.gif            System.IO.File.Delete(strTempFullName);
InBlock.gif
InBlock.gif            conn.Close();
InBlock.gif            
InBlock.gif            
return ds.Tables[0];
ExpandedBlockEnd.gif        }

下面再提供一个根据文本串在数据库上生成一个临时表的方法, 方便批量处理DataTable
 <appSettings>
 <add key="odbcstring" value="ODBC;Driver=SQL Server;UID=sa;PWD=123;Server=192.168.1.123;DataBase=Caa{0};" />
 </appSettings>

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 把文件串存储到SQL SERVER数据库临时表中
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="intYear">年份</param>
InBlock.gif        
/// <param name="strTempTableName">表名/param>
ExpandedBlockEnd.gif        
/// <param name="strDataSet">DTS文本串</param>

None.gif          public   static   void  StringToTemp( int  intYear,  string  strTempTableName,  string  strDataSet)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strTempFileName = string.Empty;
InBlock.gif            
string strTempFullName = string.Empty;
InBlock.gif            
string strPath = System.IO.Path.GetTempPath();
InBlock.gif
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTempFileName 
= GetRandomFileName(15);
InBlock.gif                strTempFullName 
= strPath + strTempFileName + ".txt";
InBlock.gif                
if (!System.IO.File.Exists(strTempFullName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            System.IO.StreamWriter sw 
= new System.IO.StreamWriter(strTempFullName, false, System.Text.Encoding.Default);
InBlock.gif            sw.Write(strDataSet);
InBlock.gif            sw.Close();        
InBlock.gif    
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
InBlock.gif                
+ "Extended Properties=Text;Persist Security Info=False", strPath));
InBlock.gif            conn.Open();
InBlock.gif
InBlock.gif            
string strODBC = string.Format(System.Configuration.ConfigurationSettings.AppSettings["odbcstring"], intYear);
InBlock.gif
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand(string.Format("select * into {0} in [OBBC][{1}] from {2}#txt"
InBlock.gif                strTempTableName, strODBC, strTempFileName), conn);
InBlock.gif
InBlock.gif            cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif            System.IO.File.Delete(strTempFullName);
InBlock.gif
InBlock.gif            conn.Close();
ExpandedBlockEnd.gif        }

下一篇文章将写出如何在Delphi中实现上面同样功能的方法, 以便两个语言之间互相传输DataTable

转载于:https://www.cnblogs.com/qianwt/archive/2006/08/14/476144.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值