日期:2014-05-16 浏览次数:20423 次
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";