Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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# 将文本文件导入SQL表_C#_Sql Server - Fatal编程技术网

C# 将文本文件导入SQL表

C# 将文本文件导入SQL表,c#,sql-server,C#,Sql Server,我有一堆两行(带标题行)“|”分隔的文本文件。我需要将它导入到一个特定的SQL表中,但我很难使用该命令 string sqltable = ("dbo.SLT_C" + "60" + "Staging"); string[] importfiles= Directory.GetFiles(@"K:\jl\load\dest", "*.txt") SqlConnection con = new SqlConnection("Data Source=" + "Cove" + ";Initial Ca

我有一堆两行(带标题行)“|”分隔的文本文件。我需要将它导入到一个特定的SQL表中,但我很难使用该命令

string sqltable = ("dbo.SLT_C" + "60" + "Staging");
string[] importfiles= Directory.GetFiles(@"K:\jl\load\dest", "*.txt")
SqlConnection con = new SqlConnection("Data Source=" + "Cove" + ";Initial Catalog=" + "GS_Ava_MCase"+ ";Integrated Security=" + "SSPI");
con.Open();
foreach (string importfile in importfiles)
{

}

或者我可能完全错了。

你可以看看现成的解决方案,比如。此免费库允许您通过描述文件中字段的类来定义文件的结构,然后您可以轻松地将整个文件加载到该类类型的数组中

完成后,只需迭代对象,并将它们保存到SQL Server

或者查看SQL Bulkcopy选项:

  • -也看到这个了吗
如果要在“直接”ADO.NET中执行此操作,请使用类似以下方法:

string sqltable = "dbo.SLT_C60Staging";

string[] importfiles = Directory.GetFiles(@"K:\jl\load\dest", "*.txt");

// try to wrap your ADO.NET stuff into using() statements to automatically 
// dispose of the SqlConnection after you're done with it
using(SqlConnection con = new SqlConnection("Data Source=Cove;Initial Catalog=GS_Ava_MCase;Integrated Security=SSPI"))
{
   // define the SQL insert statement and use parameters
   string sqlStatement = 
      "INSERT INTO dbo.YourTable(DateField, TimeField, TextField) VALUES(@Date, @Time, @Text)";

   // define the SqlCommmand to do the insert - use the using() approach again  
   using(SqlCommand cmd = new SqlCommand(sqlStatement, con))
   {
      // define the parameters for the SqlCommand 
      cmd.Parameters.Add("@Date", SqlDbType.DateTime);
      cmd.Parameters.Add("@Time", SqlDbType.DateTime);
      cmd.Parameters.Add("@Text", SqlDbType.VarChar, 1000);

      // loop through all files found
      foreach (string importfile in importfiles)
      {
         // read the lines from the text file
         string[] allLines = File.ReadAllLines(importfile);

         con.Open();

         // start counting from index = 1 --> skipping the header (index=0)
         for (int index = 1; index < allLines.Length; index++)
         {
            // split up the data line into its parts, using "|" as separator
            // items[0] = date
            // items[1] = time
            // items[2] = text
            string[] items = allLines[index].Split(new char[] { '|' });

            cmd.Parameters["@Date"].Value = items[0];
            cmd.Parameters["@Time"].Value = items[1];
            cmd.Parameters["@Text"].Value = items[2];

            cmd.ExecuteNonQuery();
         }

         con.Close();
      }
   }
}
string sqltable=“dbo.SLT\u C60Staging”;
字符串[]importfiles=Directory.GetFiles(@“K:\jl\load\dest”、“*.txt”);
//尝试使用()语句将ADO.NET内容包装成自动
//处理完SqlConnection后,请将其丢弃
使用(SqlConnection con=newsqlconnection(“数据源=Cove;初始目录=GS\u Ava\u MCase;集成安全=SSPI”))
{
//定义SQL insert语句并使用参数
字符串SQL语句=
“在dbo.YourTable(DateField、TimeField、TextField)中插入值(@Date、@Time、@Text)”;
//定义SqlCommmand来执行插入-再次使用using()方法
使用(SqlCommand cmd=newsqlcommand(sqlStatement,con))
{
//定义SqlCommand的参数
cmd.Parameters.Add(“@Date”,SqlDbType.DateTime);
cmd.Parameters.Add(“@Time”,SqlDbType.DateTime);
cmd.Parameters.Add(“@Text”,SqlDbType.VarChar,1000);
//循环查找找到的所有文件
foreach(导入文件中的字符串导入文件)
{
//从文本文件中读取行
字符串[]allLines=File.ReadAllLines(importfile);
con.Open();
//从索引=1开始计数-->跳过标题(索引=0)
for(int index=1;index

这应该行得通——您的问题太模糊了,无法确切知道行中将包含哪些数据,以及需要什么样的SQL insert语句……

您可以查看一个现成的解决方案,如。此免费库允许您通过描述文件中字段的类来定义文件的结构,然后您可以轻松地将整个文件加载到该类类型的数组中

完成后,只需迭代对象,并将它们保存到SQL Server

或者查看SQL Bulkcopy选项:

  • -也看到这个了吗
如果要在“直接”ADO.NET中执行此操作,请使用类似以下方法:

string sqltable = "dbo.SLT_C60Staging";

string[] importfiles = Directory.GetFiles(@"K:\jl\load\dest", "*.txt");

// try to wrap your ADO.NET stuff into using() statements to automatically 
// dispose of the SqlConnection after you're done with it
using(SqlConnection con = new SqlConnection("Data Source=Cove;Initial Catalog=GS_Ava_MCase;Integrated Security=SSPI"))
{
   // define the SQL insert statement and use parameters
   string sqlStatement = 
      "INSERT INTO dbo.YourTable(DateField, TimeField, TextField) VALUES(@Date, @Time, @Text)";

   // define the SqlCommmand to do the insert - use the using() approach again  
   using(SqlCommand cmd = new SqlCommand(sqlStatement, con))
   {
      // define the parameters for the SqlCommand 
      cmd.Parameters.Add("@Date", SqlDbType.DateTime);
      cmd.Parameters.Add("@Time", SqlDbType.DateTime);
      cmd.Parameters.Add("@Text", SqlDbType.VarChar, 1000);

      // loop through all files found
      foreach (string importfile in importfiles)
      {
         // read the lines from the text file
         string[] allLines = File.ReadAllLines(importfile);

         con.Open();

         // start counting from index = 1 --> skipping the header (index=0)
         for (int index = 1; index < allLines.Length; index++)
         {
            // split up the data line into its parts, using "|" as separator
            // items[0] = date
            // items[1] = time
            // items[2] = text
            string[] items = allLines[index].Split(new char[] { '|' });

            cmd.Parameters["@Date"].Value = items[0];
            cmd.Parameters["@Time"].Value = items[1];
            cmd.Parameters["@Text"].Value = items[2];

            cmd.ExecuteNonQuery();
         }

         con.Close();
      }
   }
}
string sqltable=“dbo.SLT\u C60Staging”;
字符串[]importfiles=Directory.GetFiles(@“K:\jl\load\dest”、“*.txt”);
//尝试使用()语句将ADO.NET内容包装成自动
//处理完SqlConnection后,请将其丢弃
使用(SqlConnection con=newsqlconnection(“数据源=Cove;初始目录=GS\u Ava\u MCase;集成安全=SSPI”))
{
//定义SQL insert语句并使用参数
字符串SQL语句=
“在dbo.YourTable(DateField、TimeField、TextField)中插入值(@Date、@Time、@Text)”;
//定义SqlCommmand来执行插入-再次使用using()方法
使用(SqlCommand cmd=newsqlcommand(sqlStatement,con))
{
//定义SqlCommand的参数
cmd.Parameters.Add(“@Date”,SqlDbType.DateTime);
cmd.Parameters.Add(“@Time”,SqlDbType.DateTime);
cmd.Parameters.Add(“@Text”,SqlDbType.VarChar,1000);
//循环查找找到的所有文件
foreach(导入文件中的字符串导入文件)
{
//从文本文件中读取行
字符串[]allLines=File.ReadAllLines(importfile);
con.Open();
//从索引=1开始计数-->跳过标题(索引=0)
for(int index=1;index

这应该行得通-您的问题太模糊了,无法确切知道行中将包含哪些数据,以及需要什么样的SQL insert语句…

使用文本ODBC驱动程序也可以。在ODBC管理员中,您可以选择“Microsoft Access文本驱动程序”。它允许您选择分隔符类型。设置数据源后,导入到数据表。从这里开始,将数据移动到SQL Server表中应该相当简单。

使用文本ODBC驱动程序也可以。在ODBC管理员中,您可以选择“Microsoft Access文本驱动程序”。它允许您选择分隔符类型。设置数据源后,导入到数据表。从这里开始,将数据移动到SQL Server表中应该相当简单。

文件中的字段是否总是相同的?或者每个文件包含的数据不同?用一种可能的方法更新了我的答案-它是n