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 ...
随机推荐
- MS SQL 数据库所在C盘变得很大解决办法
C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG 一般是这个文件夹里的ERRORLOG变得很大.所以要清理. 执行一次EXEC sp_cy ...
- Win7下,nginx默认80端口被System占用,造成nginx启动报错
在win7 32位旗舰版下,启动1.0.8版本nginx,显示如下错误: 2012/04/02 13:55:59 [emerg] 7864#2376: bind() to 0.0.0.0:80 fai ...
- MVC Action 返回类型
https://www.cnblogs.com/xielong/p/5940535.html https://blog.csdn.net/WuLex/article/details/79008515 ...
- 前端-JavaScript2-5——JavaScript之运算符进阶
运算符(Operators,也翻译为操作符),是发起运算的最简单形式. 运算符的分类见仁见智,我们的课程对运算符进行如下分类: 数学运算符(Arithmetic operators) 比较运算符(Co ...
- idea git commit撤销
到项目目录下,打开git bash 1.git log 找到要撤销的版本id1 commit之前的版本id2 2.git reset –hard id13.git reset id2 参考链接:htt ...
- windows下的Redis主从集群搭建
Redis官方不提供Windows版本,目前Windows下的版本是有微软开源团队(Microsoft Open Tech group)维护. http://redis.cn/ redis中文网 wi ...
- 05-Eclispe配置Tomcat插件
此插件只针对 eclipse-java-indigo-SR2-win32 这个开发工具使用的 1.下载tomcat插件 2.解压到指定位置 3.找到eclispe安装目录 D:\software\ec ...
- GitHub提供服务简介
|GitHub-Funcation| |Git仓库| 一般情况下,我们可以免费建立任意个GitHub提供的Git仓库.但需要私有仓库则需要最低每月支付$7. |Organization| 这 ...
- linux Ubuntu 16.04安装 postgresql
两次的重装系统安装数据库折磨的死去活来. 安装步骤: 1,运行 sudo apt-get update --这个用来查找数据源 2 ,运行 apt install postgresql ...
- Altium Designer 放置机械孔
先放置一个圆弧,将圆选中:执行Tools -> Convert -> Create Board Cutout from Selected Primitives