C# dataset sql questions
What I have going on is a list of ID's (nc.txt) and a list of columns (listbox1).
I am trying to export from SQL all data from the list of ID's that match the list of columns. I'm sure i dont have the dataset right. Any help aprpicated.
update: string contents = 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();
}
}
}
}
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'Aggregate' and no extension method 'Aggregate' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive or an assembly reference?)
Error 2 'System.Data.DataRowCollection' does not contain a definition for 'Aggregate' and no extension method 'Aggregate' accepting a first argument of type 'System.Data.DataRowCollection' could be found (are you missing a using directive or an assembly reference?)Well fix this:
result_table.FillSchema(database, SchemaType.Source,cols);
Looks like you intended this, variable datase instead of database:
result_table.FillSchema(datase, SchemaType.Source,cols);
DataTable.Columns
and DataTable.Rows
are not generic collections. They're of type DataColumnCollection
and DataRowCollection
, respectively. These are collection types that predate .NET 2.0; before generics in .NET 2.0, you had to create a specific class for any strongly-typed collection.
To get something compatible with LINQ methods, do one of these:
dataset.Tables[0].Columns.OfType<DataColumn>()
dataset.Tables[0].Columns.Cast<DataColumn>()
The difference is that OfType
will get only those items that can be cast to that type, and discard the rest, whereas Cast
will expect the collection to contain only items that can be cast to that type, and will throw an exception if it encounters one that can't be.
精彩评论