多文本文件读入数据库经验之认真查错
来源:互联网 发布: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(); } }}
- 多文本文件读入数据库经验之认真查错
- 使用Bulk Insert语句将文本文件读入数据库
- 认真数据库
- 一行一行读入文本文件
- 将文本文件读入c#数组
- 从文本文件逐行读入数据
- matlab读入txt文本文件数据
- 查错
- 查错
- 查错。
- ASP中查错之实例
- 关于将数据文本文件读入HashTable
- 43节从文本文件中读入数据!
- YTU 2925: 文件操作--文本文件读入
- Problem B: 文件操作--文本文件读入
- 经验分享之数据库优化
- 从数据库读入
- android 程序优化之查错篇
- 高手的自定义函数库
- JS中showModalDialog 详细使用
- 竹马青梅----爱情,敌不过流年似水
- java.net.BindException: Cannot assign requested address: JVM_Bind
- java对称加密,解密,还原原文
- 多文本文件读入数据库经验之认真查错
- 简单的HR系统
- 灵活使用XMultipleSeriesRenderer设置自定义的轴标签
- How to save brightness settings?
- oracle 数据泵
- TFTP服务器和FTP服务器
- Aspose.Words导出带图片人员信息到Word中
- EditText限制输入长度
- C# WinForm开发系列 - ListBox/ListView/Panel