110,538
社区成员
发帖
与我相关
我的任务
分享
DataRow row = table.NewRow(); //创建一个DataRow对象
row["StartTelNum"] = startTelNum;//一定要在一开始创建 table.Columns.Add("StartTelNum");
row["city"] = city;
row["TelType"] = telType;
//row["TelXuHao"] = xuHao;
table.Rows.Add(row);// NewRow只是创建,没有插入
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connStr))
{
bulkCopy.DestinationTableName = "T_TelNum";
bulkCopy.ColumnMappings.Add("StartTelNum", "StartTelNum");
bulkCopy.ColumnMappings.Add("City", "TelArea");
bulkCopy.ColumnMappings.Add("TelType", "TelType");
//添加DataTable中列名和数据库表列名的映射
//bulkCopy.ColumnMappings.Add("TelXuHao", "TelXuHao");
bulkCopy.WriteToServer(table);
}
TimeSpan ts = DateTime.Now - starTime;
MessageBox.Show(i1+"条数据" + ts.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using Microsoft.Win32;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace 手机号码导入
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnDaoRu_Click(object sender, RoutedEventArgs e)
{
string connStr=ConfigurationManager.ConnectionStrings["dbConnstr"].ConnectionString;
//找到打开导入文件
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
//判断是否选择了文件
if (ofd.ShowDialog() == false)
{
MessageBox.Show("请选择文件");
return;
}
string[] lines = File.ReadLines(ofd.FileName,Encoding.Default).ToArray();
DateTime starTime = DateTime.Now;
DataTable table = new DataTable();
table.Columns.Add("StartTelNum");
table.Columns.Add("City");
table.Columns.Add("TelType");
int i1 = 0;
for (int i = 1; i < lines.Length; i++)
{
i1++;
string line = lines[i];
string[] strs = line.Split('\t');
string startTelNum = strs[0];
string city = strs[1]; //出错点
city = city.Trim('"');//去除元素里的“”号
string telType = strs[2];
telType = telType.Trim('"');//去除元素里的“”号
string xuHao = strs[3];
xuHao = xuHao.Trim('"');//去除元素里的“”号