程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> 利用VisualBasic中TextFieldParser解析器把CSV格式倒入數據庫

利用VisualBasic中TextFieldParser解析器把CSV格式倒入數據庫

編輯:VB.NET

寫了個Demo,利用Microsoft.VisualBasic這個程序集中的TextFieldParser解析器解析CSV格式的文件,然後將解析的數據插入到相關表,這樣的好處是不用去用令人頭疼的ODBC去操作CSV格式文件,如之前是這樣去操作:

利用ODBC去操作

string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                    string sql_select;
                    OdbcConnection conn;
                    conn = new OdbcConnection(strConnString.Trim());
                    conn.Open();
                    OdbcCommand commandRowCount = new OdbcCommand("SELECT COUNT(*) FROM [" + this.FileNevCSV.Trim() + "]", conn);
                    this.rowCount = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
                    sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
                    OdbcCommand commandSourceData = new OdbcCommand(sql_select, conn);
                    OdbcDataReader dataReader = commandSourceData.ExecuteReader();
                    DataTable dt;
                    dt = dataReader.GetSchemaTable();

利用TextFieldParser操作

namespace ImportCSV
{
    class Program
    {
        //連接字符串
        private static readonly string connStr = @"Data Source=BEAR\EYESSQLSERVER;Initial Catalog=Test;Integrated Security=True";
        //表明,最好做成是客配置,如Winform程序下拉框
        private static string tableName = "Customer";
        /// <summary>
        /// 執行查詢,返回DataTable數據源
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="cmdText"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        static DataTable ExecuteDataTable(string connStr,string cmdText,params SqlParameter[] parameters)
        {
            using (SqlConnection conn=new SqlConnection(connStr))
            {
                using (SqlCommand cmd=conn.CreateCommand())
                {
                    cmd.CommandText = cmdText;
                    cmd.Parameters.AddRange(parameters);
                    using (SqlDataAdapter adapter=new SqlDataAdapter(cmd))
                    {
                        DataTable dataTable=new DataTable();
                        adapter.Fill(dataTable);
                        return dataTable;
                    }
                }
            }
        }
        /// <summary>
        /// 得到主鍵列
        /// </summary>
        /// <returns></returns>
        static List<string> GetKeyWords()
        {
            //SQL Server 系統試圖得到主鍵列
            string sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME=@TABLE_NAME";
            DataTable dt=ExecuteDataTable(connStr, sql, new SqlParameter("TABLE_NAME", tableName));
            List<string> listKeyWords=new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                string keyWord = Convert.ToString(row["COLUMN_NAME"]);
                listKeyWords.Add(keyWord);
            }
            return listKeyWords;
        }
        /// <summary>
        /// 得到指定表所有的列
        /// </summary>
        /// <returns></returns>
        static List<string> GetAllColumns()
        {
            //系統視圖得到所有列
            string sql = "select * from  INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@TABLE_NAME";
            DataTable dt = ExecuteDataTable(connStr, sql, new SqlParameter("TABLE_NAME", tableName));
            List<string> listCols = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                string columnName = Convert.ToString(row["COLUMN_NAME"]);
                listCols.Add(columnName);
            }
            return listCols;
        }
        /// <summary>
        /// 得到初主鍵外所有列
        /// </summary>
        /// <returns></returns>
        static List<string> GetAllColumnsWithoutKeyWords()
        {
    
            List<string> listAllColumns = GetAllColumns();
            List<string> listKeyWords= GetKeyWords();
            return listAllColumns.Except(listKeyWords).ToList();
        }
        /// <summary>
        /// 得到除Identity(標識)外所有列
        /// </summary>
        /// <returns></returns>
        static List<string> GetAllColumnsWithoutIdentity()
        {
            //得到Identity標志列
            string sql = @"select COLUMN_NAME
                                    from INFORMATION_SCHEMA.COLUMNS
                                    where TABLE_SCHEMA = 'dbo'
                                    and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
                                    and TABLE_NAME=@TABLE_NAME";
            DataTable dt = ExecuteDataTable(connStr, sql, new SqlParameter("TABLE_NAME", tableName));
            List<string> listColumnsWithoutIdentity = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                listColumnsWithoutIdentity.Add(Convert.ToString(row["COLUMN_NAME"]));
            }
            List<string> listAllColumns = GetAllColumns();
            //從所有列中排除
            return listAllColumns.Except(listColumnsWithoutIdentity).ToList();
        }
    
       
    
        static void Main(string[] args)
        {
                
            using (SqlConnection connection = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    //得到所有列除Identity標志列
                    string[] columnsWithoutIdentity = GetAllColumnsWithoutIdentity().ToArray();
                    //SQL參數
                    string[] columnsParameters = (from c in columnsWithoutIdentity select "@" + c).ToArray();
    
                    StringBuilder sb=new StringBuilder();
                    //拼接Insert SQL語句
                    sb.AppendLine("insert into " + tableName + "(" + string.Join(",", columnsWithoutIdentity) +
                                  ") output inserted.id values(" + string.Join(",",columnsParameters)+ ")");
                    cmd.CommandText = sb.ToString();
                    //從路徑得到csv的文件,可以做成打開框
                    using (var myCsvFile = new TextFieldParser(@"C:\Users\eyeswang\Desktop\xxxx.csv",Encoding.Default))
                    {
                        myCsvFile.TextFieldType = FieldType.Delimited;
                        myCsvFile.SetDelimiters(",");//設置解析器分割符
                        connection.Open();
                        //循環,一行一行讀
                        while (!myCsvFile.EndOfData)
                        {
                            string[] fieldArray;
                            try
                            {
                                //讀取一行
                                fieldArray = myCsvFile.ReadFields();
                                for (int i = 0; i < fieldArray.Count(); i++)
                                {
                                   //給參數賦值,如果是NULL,則DBNULL.Value插入相關列
                                    cmd.Parameters.Add("@" + columnsWithoutIdentity[i], fieldArray[i].ToUpper() == "NULL" ? (object)DBNull.Value : fieldArray[i]);
                                }
                               //執行完畢後記得Parameters Clear
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
                            {
    
                                continue;
    
                            }
    
                        }
                    }
    
                }
            }
            Console.WriteLine("OK");
            Console.ReadKey();
    
        }
    }
}

作者:cnblogs 木宛城主

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved