Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将文本文件数据导入PostgreSQL数据库#_C#_Sql_Postgresql_Text Files - Fatal编程技术网

C# 使用C将文本文件数据导入PostgreSQL数据库#

C# 使用C将文本文件数据导入PostgreSQL数据库#,c#,sql,postgresql,text-files,C#,Sql,Postgresql,Text Files,在这里,我已经完成了如何将数据转换成文本文件 有人能告诉我如何使用C#将文本文件数据导出到SQL数据库的代码吗?为什么不这样做 private void GetTextFile() { NpgsqlConnection npgsqlConnection = new NpgsqlConnection(); npgsqlConnection.ConnectionString = "Server=127.0.0.1;Port=5432;User Id=postgres;

在这里,我已经完成了如何将数据转换成文本文件


有人能告诉我如何使用C#将文本文件数据导出到SQL数据库的代码吗?

为什么不这样做

private void GetTextFile()
{
     NpgsqlConnection npgsqlConnection = new NpgsqlConnection();
     npgsqlConnection.ConnectionString = "Server=127.0.0.1;Port=5432;User 
     Id=postgres;Password=rutuparna;Database=Employee";
     npgsqlConnection.Open();

     NpgsqlCommand command = new NpgsqlCommand("Select * from employee_details", npgsqlConnection);
     NpgsqlDataReader dataReader = command.ExecuteReader();

     using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"D:\Rutu\txtfile.txt", false, Encoding.UTF8))
     {

        while (dataReader.Read())
        { 
         writer.WriteLine(dataReader[0] + "; " + dataReader[1] + ";" + dataReader[2] + ";" + dataReader[3]);
        }
     }

     MessageBox.Show("Data fetched Properly");
}

我不知道你列的名称,所以你需要替换它们

如果速度是一个考虑因素(而且总是应该考虑的),并且您正在使用
PostgreSQL
(您似乎是这样),那么您应该看看函数。单次插入总是比批量操作慢

在c#中封装
COPY
函数相对容易。下面是一个方法的简化版本,以说明这一点。我的方法循环遍历
数据表的行
,但它很容易适应文件情况(
ReadLine()
等)

使用(var pgConn=newnpgsqlconnection(myPgConnStr))
{
使用(var writer=pgConn.BeginBinaryImport(“复制”+destinationTableName+“(+commaSepFieldNames+”)从标准输入(二进制格式)”)
{
//循环浏览数据
for(int i=0;i

WriteNull()
是向流中正确添加
NULL
的特殊方法,否则使用
Write()
。后者有三个重载,仅值、类型为枚举的值或类型为名称的值。我更喜欢使用枚举。commaSepFieldNames是以逗号分隔的字段名列表。其他变量(我希望)应该是不言自明的。

您好!欢迎来到StackOverflow。这是否回答了您的问题?
using (SqlConnection con = new SqlConnection(@"your connection string"))
{
    con.Open();
    using(StreamReader file = new StreamReader(@"D:\Rutu\txtfile.txt"))
    {
         while((line = file.ReadLine()) != null)
         {
             string[] fields = line.Split(',');

             SqlCommand cmd = new SqlCommand("INSERT INTO employee_details(column1, column2, column3,column4) VALUES (@value1, @value2, @value3, @value4)", con);
             cmd.Parameters.AddWithValue("@value1", fields[0].ToString());
             cmd.Parameters.AddWithValue("@value2", fields[1].ToString());
             cmd.Parameters.AddWithValue("@value3", fields[2].ToString());
             cmd.Parameters.AddWithValue("@value4", fields[3].ToString());
             cmd.ExecuteNonQuery();
         }
    }
 }
using (var pgConn = new NpgsqlConnection(myPgConnStr))
{
    using (var writer = pgConn.BeginBinaryImport("COPY " + destinationTableName + " (" + commaSepFieldNames + ") FROM STDIN (FORMAT BINARY)"))
    {
        //Loop through data
        for (int i = 0; i < endNo; i++)
        {
            writer.StartRow();

            //inner loop through fields
            for (int j = 0; j < fieldNo; j++)
            {

                //test for null
                if (true)
                {
                    writer.WriteNull();
                }
                else
                {
                    //Write data using column types
                    writer.Write(value, type);
                }
            }
        }
        writer.Complete();
    }
}