各位,    我找到一些资料后自己写了一个读取多文本文件数据导入数据库的代码,代码执行是没问题Catch也没错误但是实际上出来的行数小于我的实际数,查了很多网上资料没找到原因。让各位大侠给指点下。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();
        }    }
}

解决方案 »

  1.   

    DataContents.Text = sr.ReadLine();
    Attline = sr.ReadLine();两个ReadLine(),是不是有问题呢?这样子肯定少读了...
      

  2.   

                        
                        try
                        {
                            StreamReader sr = new StreamReader(o);
                           
                            
                            while (( Attline = sr.ReadLine()) != 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();                    }