码迷,mamicode.com
首页 > 移动开发 > 详细

C#------手机号码归属地查询(导入数据)

时间:2015-09-05 22:00:06      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace shoujihao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Imports_Click(object sender, EventArgs e)
        {
            //文件打开对话框
            OpenFileDialog dlg = new OpenFileDialog();

            //如果用户没选择文件,就返回
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            //获得用户选择的文件
            string fileName = dlg.FileName;

            //读取所选择的文件
            using(Stream fs = new FileStream(fileName,FileMode.Open))
            using (StreamReader reader = new StreamReader(fs, Encoding.Default))
            { 
                //开始读取这个文件
                string line;
                //跳过第一行
                line = reader.ReadLine();

                //创建数据库链接
                using (MySqlConnection conn = MySqlHelper.CreateConnection())
                //事务回滚
                using(MySqlTransaction tx = conn.BeginTransaction())
                {
                    try
                    {
                        while ((line = reader.ReadLine()) != null)
                        {
                            //如果最后一行为空字符串,则跳出整个循环
                            if (string.IsNullOrEmpty(line))
                            {
                                break;
                            }

                            //把line字符串按照,号进行分割
                            string[] lines = line.Split(,);
                            //去掉字符串中的“”号
                            string MobileNumber = lines[1].Trim(");
                            string MobileArea = lines[2].Trim(");
                            string MobileType = lines[3].Trim(");

                            //插入数据库
                            //MySqlHelper.ExecuteNonQuery(conn, "insert into t_mobile(MobileNumber,MobileArea,MobileType) values(@MobileNumber,@MobileArea,@MobileType))",
                            //   new MySqlParameter { ParameterName = "@MobileNumber", Value = MobileNumber },
                            //   new MySqlParameter { ParameterName = ",@MobileArea", Value = MobileArea },
                            //   new MySqlParameter { ParameterName = "@MobileType", Value = MobileType });
                            MySqlHelper.ExecuteNonQuery(conn, "INSERT INTO t_mobile(MobileNumber,MobileArea,MobileType) VALUES(@MobileNumber,@MobileArea,@MobileType)",
                                new MySqlParameter { ParameterName = "@MobileNumber", Value = MobileNumber },
                                new MySqlParameter { ParameterName = "@MobileArea", Value = MobileArea },
                                new MySqlParameter { ParameterName = "@MobileType", Value = MobileType });
                        }
                        tx.Commit();
                    }
                    catch(Exception ex)
                    {
                        tx.Rollback();
                        MessageBox.Show("导入失败" + ex.Message);
                    }
                }
            }
            MessageBox.Show("导入成功!");
        }
    }
}

 

C#------手机号码归属地查询(导入数据)

标签:

原文地址:http://www.cnblogs.com/phpweige/p/4783862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!