c#sqlite 求教 批量插入,该怎么解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#sqlite 求教 批量插入,该怎么解决相关的知识,希望对你有一定的参考价值。
参考技术A private void btnImport_Click(object sender, EventArgs e)string filePath = textBox1.Text;
string importPwd = txtPwd.Text;
if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(importPwd))
MessageBox.Show("请先导入文件,填写操作密码后,再操作!");
else
btnImport.Text = "正在导入...";
btnImport.Enabled = false;
string[] allLines = File.ReadAllLines(filePath);
using (SQLiteConnection con = new SQLiteConnection(connStr))
con.Open();
DbTransaction trans = con.BeginTransaction();//开始事务
SQLiteCommand cmd = new SQLiteCommand(con);
try
for (int n = 0; n < allLines.Length; n++)
cmd.CommandText = "insert into imei(imei) values(@imei)";
cmd.Parameters.Add(new SQLiteParameter("@imei", DbType.String));
cmd.Parameters["@imei"].Value = allLines[n];
cmd.ExecuteNonQuery();
trans.Commit();//提交事务
MessageBox.Show("文件导入成功!");
catch (Exception ex)
trans.Rollback();
MessageBox.Show("文件导入错误,请检查是否重复导入或其它原因!");
finally
btnImport.Text = "导 入";
btnImport.Enabled = true;
/// <summary>
/// 使用SqlBulkCopy批量插入,只限SQLServer,超大数据量快速导入
/// 缺点,没有返回行数
/// </summary>
/// <param name="table">填充的DataTable,支持其它数据源,请看重载</param>
/// <param name="tableName">数据库对应表名</param>
/// <param name="columns">插入表对应的列名集合</param>
public void SqlBulkCopyInsert(DataTable table, string tableName, string[] columns)
SqlBulkCopy sbc = new SqlBulkCopy("接连字符串");
sbc.DestinationTableName = tableName;
foreach (string col in columns)
sbc.ColumnMappings.Add(col, col);
sbc.WriteToServer(table);
/// <summary>
/// 多行插入,Connection/Command/DataAdapter看你连接的数据库类型
/// 进行相应的替换即可
/// </summary>
/// <param name="ds">填充数据后的数据集</param>
/// <returns>受影响行数</returns>
public int MultyInsert(DataSet ds)
int result = 0;
IDbConnection con = new OracleConnection("连接字符串");
con.Open();
IDbCommand cmd = new OracleCommand();
cmd.CommandText = "Insert into Member(UserName,Password) values(@name,@password)";
IDbDataParameter namePar = cmd.CreateParameter();
namePar.ParameterName = "@name";
namePar.SourceColumn = "UserName";
namePar.SourceVersion = DataRowVersion.Original;
namePar.DbType = DbType.String;
cmd.Parameters.Add(namePar);
IDbDataParameter passPar = cmd.CreateParameter();
passPar.ParameterName = "@pass";
passPar.DbType = DbType.String;
passPar.SourceColumn = "Password";
passPar.SourceVersion = DataRowVersion.Original;
cmd.Parameters.Add(passPar);
IDbDataAdapter adpt = new OracleDataAdapter();
adpt.InsertCommand = cmd;
try
result = adpt.Update(ds);
catch (Exception)
throw;
finally
con.Close();
return result;
/*
* Oracle中非常强大快速的数据批量操作方法
*/
////引用
//using System.Data;
//using System.Data.OracleClient;
//using Oracle.DataAccess.Client; //Oracle自带数据访问组件 位置: $Oracle安装路径$/bin/Oracle.DataAccess.dll
//设置一个数据库的连接串
string connectStr = "User Id=scott;Password=tiger;Data Source=";
OracleConnection conn = new OracleConnection(connectStr);
OracleCommand command = new OracleCommand();
command.Connection = conn;
//到此为止,还都是我们熟悉的代码,下面就要开始喽
//这个参数需要指定每次批插入的记录数
int recc = 10000000;
command.ArrayBindCount = recc;
//在这个命令行中,用到了参数,参数我们很熟悉,但是这个参数在传值的时候
//用到的是数组,而不是单个的值,这就是它独特的地方
command.CommandText = "insert into dept values(:deptno, :deptname, :loc)";
conn.Open();
//下面定义几个数组,分别表示三个字段,数组的长度由参数直接给出
int[] deptNo = new int[recc];
string[] dname = new string[recc];
string[] loc = new string[recc];
// 为了传递参数,不可避免的要使用参数,下面会连续定义三个
// 从名称可以直接看出每个参数的含义,不在每个解释了
OracleParameter deptNoParam = new OracleParameter("deptno", OracleType.Int32);
deptNoParam.Direction = ParameterDirection.Input;
deptNoParam.Value = deptNo;
command.Parameters.Add(deptNoParam);
OracleParameter deptNameParam = new OracleParameter("deptname", OracleType.VarChar);
deptNameParam.Direction = ParameterDirection.Input;
deptNameParam.Value = dname; command.Parameters.Add(deptNameParam);
OracleParameter deptLocParam = new OracleParameter("loc", OracleType.VarChar);
deptLocParam.Direction = ParameterDirection.Input;
deptLocParam.Value = loc;
command.Parameters.Add(deptLocParam);
//在下面的循环中,先把数组定义好,而不是像上面那样直接生成SQL
for (int i = 0; i < recc; i++)
deptNo[i] = i;
dname[i] = i.ToString();
loc[i] = i.ToString();
//这个调用将把参数数组传进SQL,同时写入数据库
command.ExecuteNonQuery();本回答被提问者采纳
以上是关于c#sqlite 求教 批量插入,该怎么解决的主要内容,如果未能解决你的问题,请参考以下文章