简体   繁体   English

从SQL Server读取-需要从CSV读取

[英]Reading from SQL Server - need to read from CSV

At the moment, I source my data from a SQL serve r(2008) database. 目前,我的数据来自SQL Server r(2008)数据库。 The cyurrent method is to use a DataTable, which is then passed around and used. cyurrent方法是使用DataTable,然后将其传递并使用。

    if (parameters != null)
    {
        SqlDataAdapter _dataAdapter = new SqlDataAdapter(SqlQuery, CreateFORSConnection());
        foreach (var param in parameters)
        {
            _dataAdapter.SelectCommand.Parameters.AddWithValue(param.Name, param.Value);
        }
        DataTable ExtractedData = new DataTable(TableName);
        _dataAdapter.Fill(ExtractedData);
        return ExtractedData;
    }
    return null;

But now, the user has said that we can also get data from txt files, which have the same structure as the tables in SQL Server. 但是现在,用户已经说过,我们还可以从txt文件获取数据,该文件的结构与SQL Server中的表相同。 So, if I have a table called 'Customer', then I have a csv file with Customer. 因此,如果我有一个名为“客户”的表,那么我的客户有一个csv文件。 with the same column structure. 具有相同的列结构。 The first line in the CSV is the column name, and matches my tables. CSV的第一行是列名,与我的表匹配。

Would it be possible to read the txt file into a data table, and then run a SELECT on that data table somehow? 是否可以将txt文件读入数据表,然后以某种方式在该数据表上运行SELECT? Most of my queries are single table queries: 我的大部分查询是单表查询:

SELECT * FROM Table WHERE Code = 111

There is, however, ONE case where I do a join. 但是,在一种情况下,我会加入。 That may be a bit more tricky, but I can make a plan. 这可能有点棘手,但是我可以制定一个计划。 If I can get the txt files into data tables first, I can work with that. 如果我可以先将txt文件放入数据表,则可以使用该文件。

Using the above code, can I not change the connection string to rather read from a CSV instead of SQL Server? 使用以上代码,是否可以将连接字符串更改为从CSV而不是SQL Server读取?

First, you'll need to read the CSV data into a DataTable. 首先,您需要将CSV数据读取到DataTable中。 There are many CSV parsers out there, but since you prefer using ADO.NET, you can use the OleDB client. 那里有许多CSV解析器,但是由于您更喜欢使用ADO.NET,因此可以使用OleDB客户端。 See the following article. 请参阅以下文章。

http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-built-in-oledb-csv-parser http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-built-in-oledb-csv-parser

Joining is a bit harder, since both sets of data live in different places. 由于这两组数据都位于不同的位置,所以连接起来有点困难。 But what you can do is get two DataTables (one from each source), then use Linq to join them. 但是您可以做的是获取两个DataTable(每个来源一个),然后使用Linq将它们联接。

Inner join of DataTables in C# C#中的DataTables的内部联接

You could read the text file into a List<string> (if there is just 1 column per file), and then use LINQ to query the list. 您可以将文本文件读入List<string> (如果每个文件只有1列),然后使用LINQ查询列表。 For example: 例如:

var result = from entry in myList
    where entry == "111"
    select entry;

Of course, this example is kind of useless since all you get back is the same string you are searching for. 当然,此示例没有用,因为您得到的所有内容都是您要搜索的相同字符串。 But if there are multiple columns in the file, and they match the columns in your DataTable , why not read the file into the data table, and then use LINQ to query the table? 但是,如果文件中有多个列,并且它们与DataTable中的列匹配,为什么不将文件读入数据表,然后使用LINQ查询该表呢?

Here is a simple tutorial about how to use LINQ to query a DataTable: http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx 这是有关如何使用LINQ查询数据表的简单教程: http : //blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset的.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM