CSV匯入SQLSERVER資料庫

來源:互聯網
上載者:User

標籤:des   blog   os   檔案   資料   io   for   cti   

    /// <summary>    /// 通過檔案流的方式來讀取CSV檔案    /// </summary>    /// <param name="files">檔案名稱</param>    /// <param name="HeadYes">第一行是否為欄位標題</param>    /// <param name="span">分隔字元</param>    /// <returns></returns>    public DataTable ReadCsvFileToTable(bool HeadYes, char span)    {        //檔案路徑和檔案名稱        string files = path + fileName;        DataTable dt = new DataTable();        StreamReader fileReader = new StreamReader(files, Encoding.Default);        try        {            //是否為第一行(如果HeadYes為TRUE,則第一行為標題列)            int lsi = 0;            //列之間的分隔字元            char cv = span;            while (fileReader.EndOfStream == false)            {                string line = fileReader.ReadLine();                string[] y = line.Split(cv);                //第一行為標題列                if (HeadYes == true)                {                    //第一行                    if (lsi == 0)                    {                        for (int i = 0; i < y.Length; i++)                        {                            dt.Columns.Add(y[i].Trim().ToString());                        }                        lsi++;                    }                    //從第二列開始為資料列                    else                    {                        DataRow dr = dt.NewRow();                        for (int i = 0; i < y.Length; i++)                        {                            dr[i] = y[i].Trim();                        }                        dt.Rows.Add(dr);                    }                }                //第一行不為標題列                else                {                    if (lsi == 0)                    {                        for (int i = 0; i < y.Length; i++)                        {                            dt.Columns.Add("Col" + i.ToString());                        }                        lsi++;                    }                    DataRow dr = dt.NewRow();                    for (int i = 0; i < y.Length; i++)                    {                        dr[i] = y[i].Trim();                    }                    dt.Rows.Add(dr);                }            }        }        catch (Exception ex)        {            throw ex;        }        finally        {            fileReader.Close();            fileReader.Dispose();        }        return dt;    }
    private string conn = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
/// <summary>
/// 將指定表的資料Copy至目標資料庫 /// </summary> /// <param name="dt">資料表</param> /// <param name="outTime">逾時時間</param> /// <returns></returns> public string CopyTableToSql(DataTable dt, int outTime, string tableName) { SqlBulkCopy sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.UseInternalTransaction); sbc.BulkCopyTimeout = outTime; sbc.NotifyAfter = dt.Rows.Count; try { sbc.DestinationTableName = tableName; sbc.WriteToServer(dt); return ""; } catch (Exception ex) { return ex.Message; } finally { sbc.Close(); } }

 



 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.