【发布时间】:2009-12-09 02:19:31
【问题描述】:
我要做的是一个 ID 列表 (nc.txt) 和一个列列表 (listbox1)。
我正在尝试从 SQL 中导出与列列表匹配的 ID 列表中的所有数据。我确定我没有正确的数据集。提供任何帮助。
更新: 字符串内容 = File.ReadAllText(textBox3.Text);
string id = id = Regex.Match(contents, @"CoreDBCaseID=(?<id>\d+)").Groups["id"].Value;
string server = server = Regex.Match(contents, @"Server=(?<Server>[^;]+)").Groups["Server"].Value;
string security = security = Regex.Match(contents, "Security=(?<Security>[^;]+)").Groups["Security"].Value;
string database = database = Regex.Match(contents, "Database=(?<Database>[^\r]+)").Groups["Database"].Value;
string[] data = new string[] {
string.Format("Table={0}", id),
string.Format("Server={0}", server),
string.Format("Security={0}", security),
string.Format("Database={0}", database),
};
string sqltable = ("dbo.SLTDS_C" + id + "_Stdtable");
string ids = File.ReadAllLines(@"C:\nc.txt").Aggregate((f, s) => f + "," + s);
String cols = String.Join(",", listBox1.Items.Cast<String>().ToArray());
string sql = "select " + cols + " from sqltable where ([id] in (" + ids + "))";
{
SqlConnection con = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=" + security);
con.Open();
SqlDataAdapter tabadapter = new SqlDataAdapter(sql, con);
DataSet dataset = new DataSet("dataset");
tabadapter.FillSchema(dataset, SchemaType.Source,cols);
DataTable tbltarget;
tbltarget = dataset.Tables[cols];
string headers = dataset.Tables[0].Columns.Aggregate((f, s) => f.Name + "," + s.Name);
string sqldata = dataset.Tables[0].Rows.Aggregate((f, s) => f.Value + "," + s.Value);
string output_text = tbltarget.Columns.Cast<DataColumn>().Select(col => col.ColumnName).Aggregate((current, next) => current + "|" + next) + "\r\n"
+ tbltarget.Rows.Cast<DataRow>().Select(row => row.ItemArray.Aggregate((current, next) => current.ToString() + "|" + next.ToString())).Cast<string>().Aggregate((current, next) => current + "\r\n" + next);
{
File.WriteAllText(@"C:\outputtest.txt", output_text);
}
con.Close();
}
}
}
}
错误 1“System.Data.DataColumnCollection”不包含“Aggregate”的定义,并且找不到接受“System.Data.DataColumnCollection”类型的第一个参数的扩展方法“Aggregate”(您是否缺少使用指令还是程序集引用?)
错误 2“System.Data.DataRowCollection”不包含“Aggregate”的定义,并且找不到接受“System.Data.DataRowCollection”类型的第一个参数的扩展方法“Aggregate”(您是否缺少 using 指令或程序集参考?)
【问题讨论】:
-
你有一个错字。你的
DataSet被命名为datase,而不是database。
标签: c#