开发者

import textfile into SQL table

I have a bunch of 2 line (with header row) '|' delimited text files. I need to import this into a specific SQL table and I'm having a hard time with the command.

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)
{

}

or maybe I am going about this the whole wrong way.


You could look at a ready-made solution, like FileHelpers. This FREE library allows you to define the structure of your file by means of a class describing the fields in your file, and then you can easily load the whole file into an array of that class type.

Once that's done, just simply iterate through the objects, and save them to your SQL Server.

Or check out the SQL Bulkcopy options:

  • bcp command line utility
  • SqlBulkCopy class in ADO.NET - also see this article at SQL Team

If you want to do it in "straight" ADO.NET, use something like this approach:

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();
      }
   }
}

That should work - you're question was too vague to know exactly what data will be in the lines, and what kind of SQL insert statement you'd need...


Using the text ODBC driver might work as well. In the ODBC administrator, you can choose the "Microsoft Access Text Driver". It allows you to choose the delimiter type. After setting up the data source, import to a data table. From there, it should be fairly simple to move the data into a SQL Server table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜