【问题标题】:C# dataset sql questionsC# 数据集 sql 问题
【发布时间】: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#


【解决方案1】:

解决这个问题:

result_table.FillSchema(database, SchemaType.Source,cols);

看起来你打算这样做,可变数据而不是数据库:

result_table.FillSchema(datase, SchemaType.Source,cols);

【讨论】:

    【解决方案2】:

    DataTable.ColumnsDataTable.Rows 不是通用集合。它们的类型分别为DataColumnCollectionDataRowCollection。这些是 .NET 2.0 之前的集合类型;在 .NET 2.0 中的泛型之前,您必须为任何强类型集合创建一个特定的类。

    要获得与 LINQ 方法兼容的内容,请执行以下操作之一:

    dataset.Tables[0].Columns.OfType&lt;DataColumn&gt;()

    dataset.Tables[0].Columns.Cast&lt;DataColumn&gt;()

    不同之处在于OfType 将仅获取那些可以转换为该类型的项目,并丢弃其余的,而Cast 将期望集合包含 可以转换为的项目强制转换为该类型,如果遇到不可以的类型,将抛出异常。

    【讨论】:

    • 如果您需要帮助,您将不得不提供比这更多的解释。因为什么而“都没有用”!?你有错误吗?你的代码不会编译?你没电了?
    • 错误 1 ​​无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“字符串”。存在显式转换(您是否缺少演员表?)
    • OfTypeCast “工作”,但这并不一定意味着你放在他们后面的所有东西都有效。请记住,您会从中得到IEnumerable&lt;DataColumn&gt;IEnumerable&lt;DataRow&gt;。您需要使用适用于 DataColumns 或 DataRows 的操作。在我看来,您使用的聚合不正确。你真正想做什么?
    • 我需要导出数据库,如下所示: Column1|column2|column3|column4 X,4,3,1 Columns1()=在列表框中选择的任何列 x 4,3,1是列出与列中的任何内容匹配的行
    • columns1|columns2|column3|column4 x,4,3,1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多