C#编程还分asp.net跟winform呢。。给你个简单的winform的实例代码
首先肯定是一个按钮
OleDbConnection conn = new OleDbConnection("sql连接字符串");
OleDbTransaction tran = null;
try
{
//把TXT文本转换为datatable
string sql = string.Format("select * from [Text;Database={0}].{1}", "这个是你的txt文件路径", “收盘报表.txt");
DataTable dt = new DataTable();
OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn);
sda.Fill(dt);
if (dt == null)
{
MessageBox.Show("您导入的文档名字有误!请去掉空格或特殊字符!");
return;
}
//去掉空行
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString().Trim() == string.Empty)
{
dt.Rows.Remove(dt.Rows[i--]);
}
}
//事务开始
tran = conn.BeginTransaction();
OleDbCommand cmd = conn.CreateCommand();
cmd.Transaction = tran;
for (int i = 0; i < dt.Rows.Count; i++)
{
string[] str=dt.Rows[i][0].ToString().split(' ');//用空格区分你的三列
string sql_insert = string.Format("insert into TABLE1 (“字段1”,"字段2","字段3") values ('{0}','{1}','{2}')", str[0],str[1].str[2]);
cmd.CommandText = sql_insert;
cmd.ExecuteNonQuery();
}
tran.Commit();
}
MessageBox.Show("导入成功!");
}
catch
{
MessageBox.Show("导入失败!");
tran.Rollback();
}
finally
{
conn.Dispose();
}
如果是数据库是sqlserver,只要把OleDb换成Sql就可以了。直接编译通过不可能的,自己看着办吧
温馨提示:答案为网友推荐,仅供参考