.NET批量数据入库
/// <summary> /// 批量写入数据库 /// </summary> /// <param name="urlInfo">Url类</param> public void InsertDataSet(List<UrlInfo> urlInfos) { OracleConnection myConn = new OracleConnection(connStr); if (myConn.State != ConnectionState.Open) { myConn.Open(); } OracleDataAdapter adapter = new OracleDataAdapter(); adapter.SelectCommand = new OracleCommand("select visitid,mobile,url,startTime,endTime,desIp,desPort,agentIp,agentPort,mobileIp,mobilePort,urlTypeId,comeFrom from cn_visit where 1=0", myConn); adapter.InsertCommand = new OracleCommand(@"INSERT INTO cn_visit(visitid,mobile,url,startTime,endTime,desIp,desPort,agentIp,agentPort,mobileIp,mobilePort,urlTypeId,comeFrom) VALUES(seq_cn_visitid.Nextval,:mobile,:url,:startTime,:endTime,:desIp,:desPort,:agentIp,:agentPort,:mobileIp,:mobilePort,:urlTypeId,:comeFrom)", myConn); adapter.InsertCommand.Parameters.Add(":mobile", OracleType.VarChar, 11, "mobile"); adapter.InsertCommand.Parameters.Add(":url", OracleType.VarChar, 700, "url"); adapter.InsertCommand.Parameters.Add(":startTime", OracleType.Number, 10, "startTime"); adapter.InsertCommand.Parameters.Add(":endTime", OracleType.Number, 10, "endTime"); adapter.InsertCommand.Parameters.Add(":desIp", OracleType.VarChar, 15, "desIp"); adapter.InsertCommand.Parameters.Add(":desPort", OracleType.Number,5, "desPort"); adapter.InsertCommand.Parameters.Add(":mobileIp", OracleType.VarChar, 15, "mobileIp"); adapter.InsertCommand.Parameters.Add(":mobilePort", OracleType.Number, 5, "mobilePort"); adapter.InsertCommand.Parameters.Add(":agentIp", OracleType.VarChar, 15, "agentIp"); adapter.InsertCommand.Parameters.Add(":agentPort", OracleType.Number, 5, "agentPort"); adapter.InsertCommand.Parameters.Add(":urlTypeId", OracleType.Number, 8, "urlTypeId"); adapter.InsertCommand.Parameters.Add(":comefrom", OracleType.Number, 2, "comeFrom"); adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; adapter.UpdateBatchSize = 0; DataSet dataSet = new DataSet(); try { adapter.Fill(dataSet); for (int i = 0; i < urlInfos.Count; i++) { DataRow dr = dataSet.Tables[0].NewRow(); dr["mobile"] = urlInfos[i].Mobile; dr["startTime"] = urlInfos[i].StartTime; dr["endTime"] = urlInfos[i].EndTime; dr["url"] = urlInfos[i].Url; dr["desIp"] = urlInfos[i].DesIp; dr["desPort"] = urlInfos[i].DesPort; dr["mobileIp"] = urlInfos[i].MobileIp; dr["mobilePort"] = urlInfos[i].MobilePort; dr["agentIp"] = urlInfos[i].AgentIp; dr["agentPort"] = urlInfos[i].AgentPort; dr["urlTypeId"] = urlInfos[i].UrlTypeId; dr["comeFrom"] = urlInfos[i].ComeFrom; dataSet.Tables[0].Rows.Add(dr); } adapter.Update(dataSet.Tables[0]); } catch (Exception e) { log.Info("写入数据库失败,原因是:" + e.ToString()); } if (myConn.State == ConnectionState.Open) { myConn.Close(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。