写了一个批量载入数据的代码,没什么技术含量,留到明年用

发布于:
分类: Microsoft.Net

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
private static readonly object SyncRoot = new object();
private DataTable SummaryTable;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DirectoryInfo TheFolder = new DirectoryInfo(@”G:2014数据2014数据”);
List<FileInfo>  Files = GetChildDirectFiles(TheFolder);
foreach (FileInfo file in Files)
{
Getdata(file.FullName);
}
this.GridView1.DataSource = SummaryTable.DefaultView;
this.GridView1.DataBind();
Label1.Text = SummaryTable.Rows.Count.ToString();
}

private List<FileInfo> GetChildDirectFiles(DirectoryInfo TheFolder)
{
List<FileInfo> files = new List<FileInfo>();

foreach (DirectoryInfo folder in TheFolder.GetDirectories())
{
if (!folder.Name.EndsWith(“_”))
{
files.AddRange(GetChildDirectFiles(folder));
}
}
foreach (FileInfo file in TheFolder.GetFiles())
{
if (file.Name.ToUpper() == “T_TDD.DBF”)
{
files.Add(file);
}

}
return files;
}
private void Getdata(string dbfPath)
{
var ConnectionString = string.Format(@”Provider=VFPOLEDB.1;Data Source={0};”, dbfPath);
DataTable table = new DataTable();
using (OleDbConnection connection = new OleDbConnection(ConnectionString))
{
string sqlstr = “SELECT ksh,xm,xbdm,csny,zxmc,sfzh,tdcj FROM T_TDD ORDER BY ksh DESC”;
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(sqlstr, connection);

myDataAdapter.Fill(table);

myDataAdapter.Dispose();

}

if (SummaryTable == null || SummaryTable.Columns.Count <= 0)
{
SummaryTable = table.Copy();
SummaryTable.PrimaryKey = new DataColumn[] { SummaryTable.Columns[“KSH”] };
}
else
{
//两个结构一样的DT合并
object[] obj = new object[table.Columns.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
table.Rows[i].ItemArray.CopyTo(obj, 0);
SummaryTable.Rows.Add(obj);

}
}
}

private void SaveToTable()
{
string mdbConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:2014数据__PRINTttc.mdb;Persist

Security Info=False;”;
using (OleDbConnection connection = new OleDbConnection(mdbConnectionString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbTransaction myTrans = connection.BeginTransaction();
try
{
var commandText = “INSERT INTO [全部]([考生号],[投档成绩]) values(@KSH,@TDCJ)”;

cmd = new OleDbCommand(commandText, connection);
cmd.Parameters.Add(“@KSH”, OleDbType.VarChar, 16);
cmd.Parameters.Add(“@TDCJ”, OleDbType.Integer, 10);

cmd.Transaction = myTrans;
int recordCount = 0;
//myTrans.Begin();
foreach (DataRow row in SummaryTable.Rows)
{
float tdcj = 0;
float.TryParse(row[“tdcj”].ToString().Trim(), out tdcj);
cmd.Parameters[“@KSH”].Value = row[“ksh”].ToString().Trim();
cmd.Parameters[“@TDCJ”].Value = (int)tdcj;
recordCount += cmd.ExecuteNonQuery();

}
myTrans.Commit();
}
catch (Exception ex)
{

myTrans.Rollback();
throw ex;
}
finally
{
cmd.Dispose();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
DirectoryInfo TheFolder = new DirectoryInfo(@”G:2014数据2014数据”);
List<FileInfo> Files = GetChildDirectFiles(TheFolder);
foreach (FileInfo file in Files)
{
Getdata(file.FullName);
}
SaveToTable();
}
}

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注