日期:2014-05-18  浏览次数:21046 次

DataSet 转换为string
string 格式:
 ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
  ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
  ['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
  ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
  ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
  ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am']

------解决方案--------------------
DataSet.getXml() 然后通过正则替换处理一下
------解决方案--------------------
循环DATASET,把数据导入到叔祖。
或转化为xml
------解决方案--------------------
循环dataset里的数据拼成字符串
------解决方案--------------------
拼串儿。。循环之~
------解决方案--------------------
用两个循环,先循环DataSet的DataTable,再循环DataTable的数据行就能得到你想要的字符串了
------解决方案--------------------
自定义函数处理
------解决方案--------------------
循环拼字符串呗
------解决方案--------------------
MARK下
------解决方案--------------------
用两个循环就搞定......
------解决方案--------------------
循环操作。。。
------解决方案--------------------
顶顶啊
------解决方案--------------------
大体就是 字符串的拼接了
------解决方案--------------------
#region test data
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
dt.Columns.Add("col2", typeof(decimal));
dt.Columns.Add("col3", typeof(decimal));
dt.Columns.Add("col4", typeof(decimal));
dt.Columns.Add("col5", typeof(string));
dt.Rows.Add(new object[] {"3m Co",71.72,0.02,0.03,"9/1 12:00am" });
dt.Rows.Add(new object[] { "Alcoa Inc", 29.01, 0.42, 1.47, "9/1 12:00am" });
dt.Rows.Add(new object[] { "Altria Group Inc", 83.81, 0.28, 0.34, "9/1 12:00am" });
ds.Tables.Add(dt);
#endregion 

string strTbl = ds.Tables[0].TableName;
StringBuilder sBld = new StringBuilder();
sBld.AppendFormat("<{0}>[^<]*", strTbl);
for (int i = 0; i < dt.Columns.Count; i++)
{
sBld.AppendFormat("<{0}>([^<]+)</{0}>[^<]*", dt.Columns[i].ColumnName);
}
sBld.AppendFormat("[^<]*</{0}>", strTbl);
string reg = sBld.ToString();

string s = ds.GetXml();
s = Regex.Replace(s, reg, "['${1}',${2},${3},${4},${5}],",RegexOptions.IgnoreCase);
s = Regex.Replace(s, @"[^]]*</?\w+>[^[]*", "");
Response.Write(s);
------解决方案--------------------
晕,上面自动在<前面多加了空格
 
C# code
#region test data
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("col1", typeof(string));
        dt.Columns.Add("col2", typeof(decimal));
        dt.Columns.Add("col3", typeof(decimal));
        dt.Columns.Add("col4", typeof(decimal));
        dt.Columns.Add("col5", typeof(string));
        dt.Rows.Add(new object[] { "3m Co", 71.72, 0.02, 0.03, "9/1 12:00am" });
        dt.Rows.Add(new object[] { "Alcoa Inc", 29.01, 0.42, 1.47, "9/1 12:00am" });
        dt.Rows.Add(new object[] { "Altria Group Inc", 83.81, 0.28, 0.34, "9/1 12:00am" });