多文本文件读入数据库经验之认真查错

来源:互联网 发布:mac pro 编辑:程序博客网 时间:2024/05/18 02:47

前二天写了个代码用来读取多文本文件数据导入数据库的代码,代码执行是没问题Catch也没错误但是实际上出来的行数小于我的实际数,查了很多网上资料没找到原因。于是在CSDN的论坛中发了求助,十分钟后就看到有人回复了我问题可能出现的位置,链接CSDN求助。

另外有一篇微软提供的读写文本文件的样例可供参考如何操作文本文件,链接Microsoft.com 。


这里把代码贴出来给自己涨点经验和记性:


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Data.SqlClient;namespace HR{    public partial class frmImportTxt : Form    {        public frmImportTxt()        {            InitializeComponent();        }        private void toolStripStatusLabel1_Click(object sender, EventArgs e)        {            OpenFileDialog choseTxt = new OpenFileDialog();            choseTxt.Multiselect = true;            choseTxt.Title = "请选择文件";            choseTxt.Filter = "文本文件|*.txt";            choseTxt.FilterIndex = 1;            SqlConnection cn = new SqlConnection(Properties.Settings.Default.hrConnectionString);            SqlCommand cmd;            string sql;            string Attline;            if (choseTxt.ShowDialog() == DialogResult.OK)            {                foreach (string o in choseTxt.FileNames)                {                                        try                    {                        StreamReader sr = new StreamReader(o);                        Attline = sr.ReadLine();                                                while (Attline != null)                        {                            sql = "Insert HR_Record (Record,ID,Record_DateTime,AttNO,Record_Time) Values (@Record,@ID,@Record_DateTime,@AttNO,@Record_Time)";                            cmd = new SqlCommand(sql, cn);                            cmd.Parameters.Add("Record", SqlDbType.NVarChar).Value = Attline;                            cmd.Parameters.Add("ID", SqlDbType.Char, 10).Value = Attline.Substring(0, 10);                            cmd.Parameters.Add("Record_DateTime", SqlDbType.DateTime).Value = DateTime.Parse(Attline.Substring(15, 4) + "/" + Attline.Substring(19, 2) + "/" + Attline.Substring(21, 2));                            cmd.Parameters.Add("AttNO", SqlDbType.Char, 2).Value = Attline.Substring(23, 2);                            cmd.Parameters.Add("Record_Time", SqlDbType.Time).Value = Attline.Substring(10, 5);                            cn.Open();                            cmd.ExecuteNonQuery();                            cn.Close();                            //DataContents.Text = sr.ReadLine();                            Attline = sr.ReadLine();                        }                        sr.Close();                    }                    catch(Exception Atterror)                    {                        DataContents.Text = Atterror.Message;                        button1.Visible = true;                        button1.Text = "执行出错";                     }                    finally                    {                        button1.Visible = true;                        button1.Text = "完成执行";                    }                }            }        }        private void button1_Click(object sender, EventArgs e)        {            this.Close();        }    }}


原创粉丝点击