//读取文本文档里面的值
private void GetData()
{
//string connStr = "server=.; database=eMonPub; User ID=sa; password=123456;";
DateTime startTime = DateTime.Now;
string conn = ConfigurationManager.AppSettings["DataDB"];
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "文本文件(*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
startTime = DateTime.Now;
DataTable dt = new DataTable();
dt.Columns.Add("Qiye_ID");
dt.Columns.Add("Indicator_ID");
dt.Columns.Add("MonitorTime");
IEnumerable lines = File.ReadLines(dialog.FileName, Encoding.Default);
foreach (string str in lines)
{
string[] strs = str.Split(' ');
DataRow row = dt.NewRow();
row["Qiye_ID"] = strs[1];
row["Indicator_ID"] = strs[2].ToString().Trim('"');
row["MonitorTime"] = strs[3].ToString().Trim('"');
dt.Rows.Add(row);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
{
bulkCopy.DestinationTableName = "QF.SelfMonitorData";//表名
bulkCopy.ColumnMappings.Add("Qiye_ID", "Qiye_ID");
bulkCopy.ColumnMappings.Add("Indicator_ID", "Indicator_ID");
bulkCopy.ColumnMappings.Add("MonitorTime", "MonitorTime");
bulkCopy.WriteToServer(dt);
bulkCopy.Close();
}
TimeSpan ts = DateTime.Now.Subtract(startTime);
MessageBox.Show("一共插入" + dt.Rows.Count + "条数据,用时:" + ts.ToString());
}