.NET Web Service 中 DataSet 参数传输时, 是把 DataSet 转换成 XML 格式的串进行传输,
这样的弊端是, 数据传输过大, 还有的就别的语言写的程序很难调用
本人在一个项目过程中, 需要.NET 写 Web Service, 而客户端需要Delphi来实现
鉴于这个原因, 我通过把DataTable转换成文本串的方法,
/**/
/// <summary>
/// 把DataTable转换成文本串
/// </summary>
/// <param name="dt">DataTable对象</param>
/// <returns>返回返回的文本串</returns>
public static string DataSetToString(DataTable dt)
{
System.Text.StringBuilder sw = new System.Text.StringBuilder();
bool bFirst = true;
foreach (System.Data.DataColumn dc in dt.Columns)
{
if (!bFirst)
{
sw.Append(",");
}
bFirst = false;
sw.Append("\"" + dc.ColumnName + "\"");
}
int intColumnCount = dt.Columns.Count;
foreach (System.Data.DataRow dr in dt.Rows)
{
for (int i=0; i<intColumnCount; i++)
{
if (i != 0)
{
sw.Append(",");
}
else
{
sw.Append("\r\n");
}
if (dr[i] != System.DBNull.Value)
{
if (dt.Columns[i].DataType == typeof(string))
{
sw.Append("\"" + dr[i].ToString().Replace("\"", "\"\"") + "\"");
}
else
{
sw.Append(dr[i].ToString());
}
}
}
}
return sw.ToString();
}
/// 把DataTable转换成文本串
/// </summary>
/// <param name="dt">DataTable对象</param>
/// <returns>返回返回的文本串</returns>
public static string DataSetToString(DataTable dt)
{
System.Text.StringBuilder sw = new System.Text.StringBuilder();
bool bFirst = true;
foreach (System.Data.DataColumn dc in dt.Columns)
{
if (!bFirst)
{
sw.Append(",");
}
bFirst = false;
sw.Append("\"" + dc.ColumnName + "\"");
}
int intColumnCount = dt.Columns.Count;
foreach (System.Data.DataRow dr in dt.Rows)
{
for (int i=0; i<intColumnCount; i++)
{
if (i != 0)
{
sw.Append(",");
}
else
{
sw.Append("\r\n");
}
if (dr[i] != System.DBNull.Value)
{
if (dt.Columns[i].DataType == typeof(string))
{
sw.Append("\"" + dr[i].ToString().Replace("\"", "\"\"") + "\"");
}
else
{
sw.Append(dr[i].ToString());
}
}
}
}
return sw.ToString();
}
提供一个取随机字符串的函数
/**/ /// <summary>
/// 产生随机文件名
/// </summary>
/// <param name="length">随机文件名长度</param>
/// <returns>产生随机文件名</returns>
private static string GetRandomFileName( int length)
{
string ch = "abcdefghijklmnopqrstuvwxyz0123456789";
string strRandomFileName = string.Empty;
int intLength = ch.Length;
Random rd = new Random();
for (int i=0; i<length; i++)
{
strRandomFileName += ch[rd.Next(intLength)];
}
return strRandomFileName;
}
下面的方法是在.NET中如何把文本串转换成DataTable的方法, 此方法同样适合在其它语言中使用
/**/
/// <summary>
/// 把文件串转化成DataTable
/// </summary>
/// <param name="strDataSet">DTS文本串</param>
/// <returns>返回的DataTable</returns>
public static DataTable StringToDataSet( string strDataSet)
{
string strTempFileName = string.Empty;
string strTempFullName = string.Empty;
string strPath = System.IO.Path.GetTempPath();
while (true)
{
strTempFileName = GetRandomFileName(15);
strTempFullName = strPath + strTempFileName + ".txt";
if (!System.IO.File.Exists(strTempFullName))
{
break;
}
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(strTempFullName, false, System.Text.Encoding.Default);
sw.Write(strDataSet);
sw.Close();
OleDbConnection conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
+ "Extended Properties=Text;Persist Security Info=False", strPath));
conn.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter(string.Format("select * from {0}#txt", strTempFileName), conn);
DataSet ds = new DataSet();
cmd.Fill(ds);
System.IO.File.Delete(strTempFullName);
conn.Close();
return ds.Tables[0];
}
/// 把文件串转化成DataTable
/// </summary>
/// <param name="strDataSet">DTS文本串</param>
/// <returns>返回的DataTable</returns>
public static DataTable StringToDataSet( string strDataSet)
{
string strTempFileName = string.Empty;
string strTempFullName = string.Empty;
string strPath = System.IO.Path.GetTempPath();
while (true)
{
strTempFileName = GetRandomFileName(15);
strTempFullName = strPath + strTempFileName + ".txt";
if (!System.IO.File.Exists(strTempFullName))
{
break;
}
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(strTempFullName, false, System.Text.Encoding.Default);
sw.Write(strDataSet);
sw.Close();
OleDbConnection conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
+ "Extended Properties=Text;Persist Security Info=False", strPath));
conn.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter(string.Format("select * from {0}#txt", strTempFileName), conn);
DataSet ds = new DataSet();
cmd.Fill(ds);
System.IO.File.Delete(strTempFullName);
conn.Close();
return ds.Tables[0];
}
下面再提供一个根据文本串在数据库上生成一个临时表的方法, 方便批量处理DataTable
<appSettings>
<add key="odbcstring" value="ODBC;Driver=SQL Server;UID=sa;PWD=123;Server=192.168.1.123;DataBase=Caa{0};" />
</appSettings>
/**/ /// <summary>
/// 把文件串存储到SQL SERVER数据库临时表中
/// </summary>
/// <param name="intYear">年份</param>
/// <param name="strTempTableName">表名/param>
/// <param name="strDataSet">DTS文本串</param>
public static void StringToTemp( int intYear, string strTempTableName, string strDataSet)
{
string strTempFileName = string.Empty;
string strTempFullName = string.Empty;
string strPath = System.IO.Path.GetTempPath();
while (true)
{
strTempFileName = GetRandomFileName(15);
strTempFullName = strPath + strTempFileName + ".txt";
if (!System.IO.File.Exists(strTempFullName))
{
break;
}
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(strTempFullName, false, System.Text.Encoding.Default);
sw.Write(strDataSet);
sw.Close();
OleDbConnection conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
+ "Extended Properties=Text;Persist Security Info=False", strPath));
conn.Open();
string strODBC = string.Format(System.Configuration.ConfigurationSettings.AppSettings["odbcstring"], intYear);
OleDbCommand cmd = new OleDbCommand(string.Format("select * into {0} in [OBBC][{1}] from {2}#txt",
strTempTableName, strODBC, strTempFileName), conn);
cmd.ExecuteNonQuery();
System.IO.File.Delete(strTempFullName);
conn.Close();
}
下一篇文章将写出如何在Delphi中实现上面同样功能的方法, 以便两个语言之间互相传输DataTable