给你个辅助类吧
model类 你自己改成你自己的 查询的列名你自己修改下即可
日志 ibLog 可以直接注释 你也可以改成你自己的日志 或者注释掉 不显示
有问题直接留言
public class IBDataBase
{
public virtual int ExecuteCommand(string safeSql)
{
return 0
}
public virtual DataTable ExecuteSqlGetDataTable(string safeSql)
{
return null
}
public virtual int ExecuteDropTable(string safeSql)
{
return 0
}
}
//DBHelper.cs
public class IBMySQLDataBase : IBDataBase
{
//数据库连接属性,从config配置文件中获取连接字符串connectionString
public string ConnectionString { getset}
/// <summary>
/// 执行无参SQL语句,并返回执行记录数
/// </summary>
/// <param name="safeSql">sql字符串</param>
/// <returns>受影响的行数</returns>
public override int ExecuteCommand(string safeSql)
{
int res = 0
if (string.IsNullOrEmpty(ConnectionString) == true)
{
return res
}
MySqlConnection sqlConn = new MySqlConnection(ConnectionString)
try
{
sqlConn.Open()
}
catch (Exception es)
{
IBLogManager.Instance.LogException(es)
sqlConn.Close()
sqlConn.Dispose()
return res
}
MySqlCommand cmd = new MySqlCommand(safeSql, sqlConn)
cmd.CommandTimeout = 600
try
{
res = cmd.ExecuteNonQuery()
}
catch (Exception es)
{
IBLogManager.Instance.Log("SQL" + safeSql + es.StackTrace)
}
finally
{
cmd.Dispose()
sqlConn.Close()
sqlConn.Dispose()
}
return res
}
/// <summary>
/// 执行SQL语句,并返回DataTable对象
/// </summary>
/// <param name="safeSql">SQL语句</param>
/// <returns>返回DataTable</returns>
public override DataTable ExecuteSqlGetDataTable(string safeSql)
{
DataTable dataTable = new DataTable()
if (string.IsNullOrEmpty(ConnectionString) == true)
{
return dataTable
}
DataSet ds = new DataSet()
MySqlConnection sqlConn = new MySqlConnection(ConnectionString)
try
{
sqlConn.Open()
}
catch (Exception es)
{
IBLogManager.Instance.Log("SQL" + safeSql + es.StackTrace)
sqlConn.Close()
sqlConn.Dispose()
return dataTable
}
MySqlCommand cmd = new MySqlCommand(safeSql, sqlConn)
cmd.CommandType = CommandType.Text
MySqlDataAdapter da = new MySqlDataAdapter(cmd)
da.Fill(ds)
if (ds != null &&ds.Tables != null &&ds.Tables.Count >0)
{
dataTable = ds.Tables[0]
}
da.Dispose()
cmd.Dispose()
sqlConn.Close()
sqlConn.Dispose()
return dataTable
}
}
public class DAL
{
private IBDataBase m_mySQLDataBaseMain = new IBMySQLDataBase() { ConnectionString = "Data Source=10.0.0.2Initial Catalog=ib_sn_dataPersist Security Info=TrueUser ID=rootPassword=rootPort=3306Allow User Variables=True" }
public IBDataBase GetDataBase()
{
return m_mySQLDataBaseMain
}
public int ExecuteCommand(string strSQL)
{
int res = 0
IBDataBase dataBase = GetDataBase()
if (dataBase == null)
{
return res
}
try
{
res = dataBase.ExecuteCommand(strSQL)
}
catch (Exception es)
{
//IBLogManager.Instance.Log(IBError_Tag.ERROR_IBDBC.ToString() + "SQL" + strSQL + es.StackTrace, IBLogManager.LogLevel.IBS_Error)
}
return res
}
public List<SNNoModel>GetSNNoModelList(string strSQL)
{
List<SNNoModel>currentTableList = new List<SNNoModel>()
IBDataBase dataBase = GetDataBase()
if (dataBase == null)
{
return currentTableList
}
DataTable dataTable = dataBase.ExecuteSqlGetDataTable(strSQL)
if (dataTable != null)
{
foreach (DataRow item in dataTable.Rows)
{
SNNoModel tableNameItem = new SNNoModel()
try
{
tableNameItem.Id = int.Parse(item["id"].ToString())
tableNameItem.FullSNNo = item["FullSNNo"].ToString()
tableNameItem.SNWipe0 = item["SNWipe0"].ToString()
tableNameItem.Brand = item["Brand"].ToString()
}
catch (ArgumentException e)
{
IBLogManager.Instance.LogException(e)
}
catch (Exception e)
{
IBLogManager.Instance.LogException(e)
}
if (tableNameItem != null)
{
currentTableList.Add(tableNameItem)
}
}
}
return currentTableList
}
public List<string>GetTableNamesFromDB(string strSQL, string colName)
{
List<string>currentTableList = new List<string>()
IBDataBase dataBase = GetDataBase()
if (dataBase == null)
{
return currentTableList
}
DataTable dataTable = dataBase.ExecuteSqlGetDataTable(strSQL)
if (dataTable != null)
{
foreach (DataRow item in dataTable.Rows)
{
object tableNameItem = null
try
{
tableNameItem = item[colName]
}
catch (ArgumentException e)
{
IBLogManager.Instance.LogException(e)
}
catch (Exception e)
{
IBLogManager.Instance.LogException(e)
}
if (tableNameItem != null)
{
currentTableList.Add(tableNameItem.ToString())
}
}
}
return currentTableList
}
}
//查询的方法//@RequestMapping("/out/toList")
//public ModelAndView toList(Map map){
//String hql=" from out_batchEntity "
//List<out_batchEntity>list=this.baseEbi.query(hql, null, "batEnd desc")
////给每个批次添加总数
//for(int i=0i<list.size()i++){
//Set<BookListEntity>set=list.get(i).getBle()
//int count=0
// for(BookListEntity bl:set){
// // count+=bl.getBkCount()
// }
//list.get(i).setBatCount(count)
//}
//map.put("list", list)
//return new ModelAndView("out/group/groupList",map)
//}
<c:forEach items="${list}" var="list" varStatus="s">
<tr>
<td>${s.index+1 }</td>
<td>${list.batName }</td>
<td>${list.batNumber }</td>
<td>${list.cmp.cyName }</td>
<td>${list.batCount}</td>
<td>${list.batStart }</td>
<td>${list.batEnd }</td>
<td><a href="#" id="bookstate">书籍状态</a><a href="#">修改</a><a href="#">删除</a></td>
</tr>
</c:forEach>
感觉和你说的差不多你试试
有两个方法可供选择,一种是把数据存入csv文件,然后执行load data infile还有一种就是类似于sql server里面的bulk insert,使用insert语句插入批量数据,结合PHP的implode函数,
可以很简单的实现大批量数组数据的一次性插入。
[php] view plaincopy
$statement = "INSERT INTO table (title, type, customer) VALUES "
foreach( $data as $row) {
$statement .= ' ("' . implode($row, '","') . '")'
}
不过大批量数据的插入,需要注意mysql在内存上有限制:
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_bulk_insert_buffer_size
bulk_insert_buffer_size变量的默认大小为8M, 是指单个线程占用的大小限制,设置为0,表示不做限制。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)