当前位置:编程学习 > C#/ASP.NET >>

求助把下面代码改成,把csv文件存在已经建立好的sqlist数据库里!!

private void button1_Click(object sender, EventArgs e)
        {
            subImportCsv_Dataset("C:\\data\\","AHU1_数据记录0.csv");
            csvToSql("C:\\data\\","AHU1_数据记录0.csv");
        }
        public void subImportCsv_Dataset(string filePath, string fileName)
        {
            string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=";
            strConn += filePath;//这个地方只需要目录就可以了                                                      
            strConn += ";Extensions=asc,csv,tab,txt;";
            OdbcConnection objConn = new OdbcConnection(strConn);
 
                string strSQL = "select * from " + fileName;//文件名,不要带目录
                OdbcDataAdapter da = new OdbcDataAdapter(strSQL, objConn);
                DataSet ds = new DataSet();
                da.Fill(ds);              
 
        }
        private static bool csvToSql(string filePath, string fileName)
        {
            string StrConn = "Data Source=.;AttachDbFilename=|DataDirectory|\\App_Data\\Database1.mdf;Integrated Security=True;User Instance=True";
             
            SqlConnection connection = new SqlConnection(StrConn);
 
            try
            {
                connection.Open();
 
                string cmdtxt = @"bulk insert Table from '" + filePath + fileName + "' with(FIRE_TRIGGERS,FIELDTERMINATOR=',')";
 
                SqlCommand cmd = new SqlCommand(cmdtxt, connection);
 
                cmd.ExecuteNonQuery();
 
                return true;
            }
            catch (System.Exception ex)
            {
                // log.WriteLine("文件:{0}  error:{1}", fileName, ex.Message);
                // log.Flush();
                return false;
            }
 
        }
数据库
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,