|
ql = string.Format("update {0} set {1} {2}", className,
values,id);
OleDbDataAdapter da = new OleDbDataAdapter();
da.UpdateCommand = new OleDbCommand(sql, conn);
da.UpdateCommand.CommandText = sql;
conn.Open();
da.UpdateCommand.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
finally
{
conn.Close();
}
}
//根据条件删除数据
public bool Delete(string where)
{
try
{
var type = typeof(T);
var className = type.Name;
var sql = string.Format("delete from {0} {1}", className,
where);
OleDbDataAdapter da = new OleDbDataAdapter();
da.DeleteCommand = new OleDbCommand(sql, conn);
da.DeleteCommand.CommandText = sql;
conn.Open();
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
finally
{
conn.Close();
}
}
//批量插入数据
public bool InsertList(List entitysList) where T : class,new()
{
try
{
var type = typeof (T);
var className = type.Name;
var fields = "";
var values = "";
foreach (var property in type.GetProperties())
{
if (property.Name.Equals("ID")) continue;
fields += "," + property.Name;
var isNumStr = (property.PropertyType == typeof (double) ||
property.PropertyType == typeof (int))
? ""
: "'";
values += ",?" ;
}
fields = fields.Substring(1);
values = values.Substring(1);
var sql = string.Format("insert into {0}({1}) values ({2}) ", className,
fields, values);
OleDbDataAdapter da = new OleDbDataAdapter();
da.InsertCommand = new OleDbCommand(sql, conn);
da.InsertCommand.CommandText = sql;
foreach (var property in type.GetProperties())
{
if (property.Name.Equals("ID")) continue;
var oleType = (property.PropertyType == typeof(double) ||
property.PropertyType == typeof(int))
? OleDbType.Integer
: OleDbType.VarChar;
da.InsertCommand.Parameters.Add(property.Name, oleType, int.MaxValue,
property.Name);
fields += "," + property.Name;
values += ",?";
}
var table = ConverterUtil.ConvertListToDataTable(entitysList);
table.TableName = className;
da.Update(table);
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
finally
{
conn.Close();
}
}
//这个方法是用来执行无返回结果的插入语句,如 insert select
public bool ExecuteInsertSql(string sql)
{
try
{
OleDbDataAdapter da = new OleDbDataAdapter();
da.InsertCommand = new OleDbCommand(sql, conn);
da.InsertCommand.CommandText = sql;
conn.Open();
da.InsertCommand.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
finally
{
conn.Close();
}
}
}
}
??
?
|