txt文本导入sql数据库 就报一个错

txt文本导入sql数据库 就报一个错,第1张

SQL Server 2005 开始,那个导入导出向导与 SQL Server 2000 的不一样。以文本文件(.txt,.csv)导入数据库表格为例,默认情况下,新版导入导出向导是默认取文本文件的前 200 行数据(在选择平面数据源-高级-建议类型里面可以更改行数),来决定每一个字段的(最小)数据类型,然后导入时将文本文件字段的数据类型转换为数据表相应字段的数据类型。这样就可能发生截断和类型转换出错。

 /// <summary>

    /// 创建并获取目录,主要用于用户文件上传的目录,目录创建在resources/upload下

    /// </summary>

    /// <param name="customize">自定义类别可以是空NULL,此参数不为空时在upload下会再新建一层目录,如:adminPic则返回../resources/upload/adminPic/年/月/</param>

    /// <returns>../resources/upload/adminPic/年/月/</returns>

    public static string getAndCreatePath(string customize)

    {

        string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/resources/upload/" + ((customize == string.Empty) ? (customize.ToString() + "/") : ("")) + DateTime.Now.Year + "/" + DateTime.Now.Month + "/")

        try

        {

            if (!File.Exists(path))

            {

                Directory.CreateDirectory(path)

            }

            return path

        }

        catch (Exception)

        {

            return HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["errorPath"]).ToString()

        }

    }

    /// <summary>

    /// 文本数据导入

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void But_importChinaBank_Click(object sender, EventArgs e)

    {

       

       if (flChinaBank.PostedFile.FileName == "")

        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "<script>alert('导入的文本文件不存在!')</script>")

        }

        else

        {

            string FilePath = getAndCreatePath("")

            string FileName = DateTime.Now.Ticks.ToString()

            //string tableName

            string postFileName//后缀名

            string newName

            try

            {

                int pos = flChinaBank.PostedFile.FileName.LastIndexOf(".") + 1

                postFileName = flChinaBank.PostedFile.FileName.Substring(pos, flChinaBank.PostedFile.FileName.Length - pos)//后缀名

                newName = FilePath + FileName + "." + postFileName

                flChinaBank.PostedFile.SaveAs(newName)

            }

            catch (Exception exec)

            {

                throw (exec)

            }

               FileStream fs = new FileStream(newName, FileMode.Open, FileAccess.Read)

                StreamReader m_streamReader = new StreamReader(fs)

                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin)

                string value = m_streamReader.ReadLine()

                string[] stringSeparators = new string[] { " " }

                string sql = ""

                int nonquery = 0

                for (int i = 0 value != null i++)

                {

                   

                    //value = value.Replace(",", "")

                    string[] array = value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)

                    sql += string.Format("insert into L_POSImportInfo_Share values('{0}','{1}','{2}','{3}','{4}','{5}',{6},{7},{8},'{9}','{10}','{11}','{12}','{13}','{14}','{15}') ",

                        0, 104370157225027, array[0].ToString(), (array[3].Substring(4, 4) + array[4]).ToString(), array[2], "无", double.Parse(array[5].Replace(",", "").ToString()) * 100.00,

    double.Parse(array[6].ToString()) * 100.00, double.Parse(array[7].ToString()) * 100.00, "无", double.Parse(array[8].ToString()) * 100.00, "无", "无", "", 0,DateTime.Now.ToString("s"))

                   

                }

                nonquery = baser.ExecuteSQL(sql)

                if (nonquery > 0)

                {

                  

                     

                     Page.ClientScript.RegisterStartupScript(this.GetType(),Guid.NewGuid().ToString(), "<script>alert('导入成功')</script>")

                     GridView1.EditIndex = -1

                     dataDind(0)

                }

                else

                {

                    

                    Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script>alert('导入失败!')</script>")

                    

                }

                m_streamReader.Close()

                fs.Close()

               

                //删除上传的文件

               if (File.Exists(newName))

               {

                  File.Delete(newName)

               }

              

        }

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9915196.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存