[ 登录注册 ]

研发管理

.net web批量导入txt数据到mssql

2016-08-25 22:10:59 admin 返回上一页

 

主要代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;

namespace testDataSet
{
    public partial class TxtToSQL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void ImportData()
        {
            if (FileUpload1.HasFile)
            {
                    using (FileStream fs = File.OpenRead(FileUpload1.PostedFile.FileName))
                    {
                        if (Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower() != ".txt")
                        {
                            msg("请选择txt文件!"); return;
                        }
                        using (StreamReader sr = new StreamReader(fs,Encoding.Default))//编码要写上,不然插入的是乱码!
                        {
                          //  Encoding.Default;//新建的文本文档,不需要改格式,windows下默认是ASCLL格式
                          //  Encoding.UTF8   //另存为->下面的"编码" 选择 "UTF-8"
                            string connstr = ConfigurationManager.ConnectionStrings["dbtest"].ToString();
                            using (SqlConnection conn = new SqlConnection(connstr))
                            {

                                conn.Open();
                                using (SqlCommand cmd = conn.CreateCommand())
                                {

                                    cmd.CommandText = "insert into myusers(uname,uage,uemail,utel,uinfo) values(@uname,@uage,@uemail,@utel,@uinfo)";
                                    string line = null;
                                    while ((line = sr.ReadLine()) != null)
                                    {
                                        msg("t4");
                                        string[] strs = line.Split('|');
                                        msg(strs.Length.ToString());
                                        if (strs.Length != 5) throw new Exception("数据格式不对!请检查导入数据文件!");
                                        string uname = strs[0];
                                        Int16 uage = Convert.ToInt16(strs[1]);
                                        string uemail = strs[2];
                                        string utel = strs[3];
                                        string uinfo = strs[4];
                                        cmd.Parameters.Clear();
                                        cmd.Parameters.Add(new SqlParameter("uname", uname));
                                        cmd.Parameters.Add(new SqlParameter("uage", uage));
                                        cmd.Parameters.Add(new SqlParameter("uemail", uemail));
                                        cmd.Parameters.Add(new SqlParameter("utel", utel));
                                        cmd.Parameters.Add(new SqlParameter("uinfo", uinfo));
                                        cmd.ExecuteNonQuery();
                                    }
                                }
                            }
                        }
                        msg("导入成功!");
                    }
            }
            else
            {
                msg("请选择文件!");
            }
        }
        protected void msg(string Msg)
        {
            Response.Write("<script>alert('"+Msg+"');</script>");
        }

        protected void btn_import_Click(object sender, EventArgs e)
        {
            try
            {
                ImportData();
            }
            catch (Exception er)
            {
                Response.Write("出错,原因:"+er.Message);
            }

        }
    }
}

 

欢迎大家到我的新浪博客学习 : http://blog.sina.com.cn/pukuimin 

微笑


文章来源:http://www.bozhiyue.com/software/2016/0825/433272.html
返回上一页    返回分类 上一篇:   下一篇:
相关