0 امتیاز

من یه فایلی دارم به که در هر خطش یه سری کد هست. سریع ترین روش ممکن برای اینکه این کد ها رو وارد یکی از ستون های دیتابیس بکنم چیه؟

در حال حاضز یه متد نوشتم و بعد با حلقه foreach دونه دونه insert میکنم توی دیتابیس. ولی خیلی طول میکشه.

        public bool Insert(string name, string productID, string company)
        {
            SqlConnection connection = new SqlConnection(myConnection);

            try
            {
                string query = "Insert Into Archive (ProductID,Name,Company) values (@ProductID,@Name,@Company)";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@ProductID", productID);
                command.Parameters.AddWithValue("@Name", name);
                command.Parameters.AddWithValue("@Company", company);
                connection.Open();
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

راه سریغ تری نیست؟

بسته شده

1 پاسخ

0 امتیاز
 
بهترین پاسخ

سلام

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

 

سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,924 کاربر

دسته بندی ها

...