C# 读取TXT文本数据 添加到数据库
protected void Button1_Click(object sender, EventArgs e)
{
//使用FileStream读取文件
FileStream fileStream = File.OpenRead(FileUpload1.PostedFile.FileName);
StreamReader reader = new StreamReader(fileStream);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
conn.Open();
//向数据库插入数据
SqlCommand command = conn.CreateCommand();
command.CommandText = insertsql;
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] str = line.Split('|');
if (str.Length<13)//跳过第一行数据
{
continue;
}
string RefParma = str[0];
string requestid = str[1];
string agency_name = str[2];
string channel_coding = str[3];
string phone = str[4];
string province_code = str[5];
string cities_code = str[6];
string TrafficSts = (str[7] == "20101") ? "1" : "-1";
string product_type_code = str[8];
string package_name = str[9];
string processsing_date = str[10];
string processsing_time = str[11];
string money = str[12];
//RefParma, requestid, agency_name, channel_coding, phone, province_code, cities_code, TrafficSts, product_type_code, package_name, processsing_date, processsing_time, money
command.Parameters.Clear(); //每次插入都要清除参数
command.Parameters.Add(new SqlParameter("RefParma", RefParma));
command.Parameters.Add(new SqlParameter("requestid", requestid));
command.Parameters.Add(new SqlParameter("agency_name", agency_name));
command.Parameters.Add(new SqlParameter("channel_coding", channel_coding));
command.Parameters.Add(new SqlParameter("phone", phone));
command.Parameters.Add(new SqlParameter("province_code", province_code));
command.Parameters.Add(new SqlParameter("cities_code", cities_code));
command.Parameters.Add(new SqlParameter("TrafficSts", TrafficSts));
command.Parameters.Add(new SqlParameter("product_type_code", product_type_code));
command.Parameters.Add(new SqlParameter("package_name", package_name));
command.Parameters.Add(new SqlParameter("processsing_date", processsing_date));
command.Parameters.Add(new SqlParameter("processsing_time", processsing_time));
command.Parameters.Add(new SqlParameter("money", money));
int tem=command.ExecuteNonQuery();
}
Response.Write("<script>alert('数据导入完成');</script>");
fileStream.Close();
reader.Close();
conn.Dispose();
}
public string insertsql = "insert into Order_Table (RefParma,requestid,agency_name,channel_coding,phone,province_code,cities_code,TrafficSts,product_type_code,package_name,processsing_date,processsing_time,money) values (@RefParma, @requestid, @agency_name, @channel_coding, @phone, @province_code, @cities_code, @TrafficSts, @product_type_code, @package_name, @processsing_date, @processsing_time, @money)";
}
C# 读取TXT文本数据 添加到数据库的更多相关文章
- Yii读取TXT文件数据插入到数据库
个人平时会用到的方法,记录一下并分享给需要的朋友,Yii批量添加还需要clone一下model才可以,不然只会插入一条数据. 也可以把文件通过参数的方式调入到方法中. // 读取CVS文件 funct ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- MATLAB读取写入文本数据最佳方法 | Best Method for Loading & Saving Text Data Using MATLAB
MATLAB读取文件有很多方法.然而笔者在过去进行数据处理中,由于函数太多,相互混杂,与C#,Python等语言相比,反而认为读取文本数据比较麻烦.C#和Python等高级语言中,对于大部分的文本数据 ...
- C#实现大数据量TXT文本数据快速高效去重
原文 C#实现大数据量TXT文本数据快速高效去重 对几千万的TXT文本数据进行去重处理,查找其中重复的数据,并移除.尝试了各种方法,下属方法是目前尝试到最快的方法.以下代码将重复和不重复数据进行分文件 ...
- .NET 利用反射将对象数据添加到数据库
.NET 利用反射将对象数据添加到数据库 一些小型的项目,在不使用其他的框架(LINQ,NHibernate,EF等等框架)的前提下,这时候一些反复的增删改查就会让我们感到极其的繁琐,厌烦,为了避 ...
- python3 读取txt文件数据,绘制趋势图,matplotlib模块
python3 读取txt文件数据,绘制趋势图 test1.txt内容如下: 时间/min cpu使用率/% 内存使用率/% 01/12-17:06 0.01 7.61 01/12-17:07 0.0 ...
- C#读取Txt大数据并更新到数据库
环境 Sqlserver 2016 .net 4.5.2 目前测试数据1300万 大约3-4分钟.(限制一次读取条数 和 线程数是 要节省服务器资源,如果调太大服务器其它应用可能就跑不了了), Sql ...
- 使用java读取解析txt文本数据,管理简单的数据
在实际开发中会经常碰到使用编程语言读取文本文件的内容,这内容可以是各种各样的一下本人写出我自己做的一个读取文本文件的例子,文件中存储的是我的个人网站 www.yzcopen.com 导航栏目因为懒得使 ...
- 用C#实现的两个试验编程(txt文本数据的导入,和数据导出为TXT)
1. 文件的存取 数据文件1.txt为学生成绩统计表,要对它做一个统计工作.文件中若一个人有多个成绩,则取他们的最好成绩来统计,然后计算全班平均成绩:统计0-69.70-79.80-89.90-100 ...
随机推荐
- 申请Let's Encrypt永久免费SSL证书
Let's Encrypt简介 Let's Encrypt作为一个公共且免费SSL的项目逐渐被广大用户传播和使用,是由Mozilla.Cisco.Akamai.IdenTrust.EFF等组织人员发起 ...
- windows下的端口转发命令netsh
使用下面的命令查看语法 netsh interface portproxy add v4tov4 /? add v4tov4 [listenport=]<integer>|<serv ...
- P1041 传染病控制(dfs)
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- Java代码片段——向文件末尾添加内容
BufferedWriter out = null; try { out = new BufferedWriter(new FileWriter(”filename”, true)); out.wri ...
- 基于墨刀实现的购物app
在观察研究购物app后 仔细分析总结了几个基础功能,实现如下的界面: 主界面:购物app的推荐界面,提供各式的样品,将整个app的多种功能进行展示. 推荐分类:根据用户浏览的宝贝,对用户进行合理推荐. ...
- nginx mac
==> nginx Docroot is: /usr/local/var/www The default port has been set in /usr/local/etc/nginx/ng ...
- 如何实现 C/C++ 与 Python 的通信?
属于混合编程的问题.较全面的介绍一下,不仅限于题主提出的问题.以下讨论中,Python指它的标准实现,即CPython(虽然不是很严格) 本文分4个部分 1. C/C++ 调用 Python (基础篇 ...
- python 列表生成式,生成器&迭代器
列表生成式: 需求:要对列表 [0,1,2,3,4,5,6,7,8,9]的每个元素加1 用列表生成式一步搞定: li = [i+1 for i in range(10)] # 这种写法就叫列表生成式 ...
- wdk1703+vs2015编译的诡异问题
最近将wdk升级到1703(10.0.15063.0)版本,编译一个新建的minifiter项目居然出现了失败 提示错误为 WindowsDriver.common.targets(460,5): e ...
- excel 拷贝数据到 plsql 报 is not a valid date and time
按以下格式设置时间格式