繁体   English   中英

如何使用c#控制台应用程序将CSV文件导出到SQL Server

[英]How to Export CSV file to SQL Server using c# Console Application

SqlServer数据库中的表名称: MyTable

Csv档案位置: @“ d:MyFile.csv”

如何使用C#控制台应用程序将CSV文件“ @” d:MyFile.csv 复制到“ MyTable ”,该表存在于SQL Server数据库中?

我已使用以下C#代码从数据库导出到CSV文件。 但是如何做逆向任务呢!

        string strConn = "Data Source=MYSERVER;Initial Catalog=Master;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strConn);
        SqlDataAdapter da = new SqlDataAdapter("select * from QuickBook", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "QB");
        DataTable dt = ds.Tables["QB"];
        StreamWriter sw = new StreamWriter(@"d:MyFile.csv", false);
        int iColCount = dt.Columns.Count;

        for (int i = 0; i < iColCount; i++)
        {
            sw.Write(dt.Columns[i]);
            if (i < iColCount - 1)
            {
                sw.Write(",");
            }
        }

        sw.Write(sw.NewLine);

        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < iColCount; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                   sw.Write(dr[i].ToString());
                }

                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }

            sw.Write(sw.NewLine);
        }

        sw.Close();

    }
}

请参阅此网站上的Codeproject链接类似问题

首先,您不想“将CSV文件导出到SQL Server”,而是要“从SQL Server导出CSV文件”。 这是不一样的。

我使用EntityFramework从日期基准生成模型。 您只需要花费两分钟即可生成模型。

添加新项>数据> ADO.Net实体数据模型>从数据库生成并选择您的连接(或建立一个新连接)。 下一步是选择要导出的表。

最后,您只需要检查数据上下文(DbSet)的属性,当然您就可以使用LINQ过滤元素。

string path = Path.Combine("D", "MyFile.csv");

using (var dataContext = new MyDataContext())
{
    using (StreamWriter sw = new StreamWriter(path))
    {
        StringBuilder line = new StringBuilder();
        foreach (var quickBook in dataContext.QuickBooks)
        {
            line.AppendFormat("{0};", quickBook.Name);
            // Append the other properties (remember the culture with the numbers)

            sw.WriteLine(line.ToString());
            line.Clear();
        }
    }
}

一个建议:您可以遍历一个类的所有属性,在您的情况下为“ QuickBook”:

foreach (PropertyInfo property in typeof(QuickBook).GetProperties())
{
    object value = property.GetValue(quickBook, null);
    if (value == null)
        line.Append(";");
    else
        line.AppendFormat("{0};", value);
}

if (line.Length > 0) //Removes the last ';'
    line.Remove(line.Length - 1, 1);

使用EntityFramework的反向任务可能是这样的:

string path = Path.Combine("D", "MyFile.csv");

using (var dataContext = new MyDataContext())
{
    using (StreamReader stream = new StreamReader(path))
    {
        string line;

        while ((line = stream.ReadLine()) != null)
        {
            string[] columns = stream.ReadLine().Split(';');

            QuickBook item = new QuickBook();
            item.Name = columns[0];
            // And so on with the other properties...

            dataContext.QuickBooks.Add(item);
        }
    }

    dataContext.SaveChanges();
}

暂无
暂无

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

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