using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WeighBusiness.Utils.SqlUtils {//TODO:检查所有操作数据库的方法,它们的返回值如何设定。现在的设定有冲突,不能反映出是操作数据库时出错,还是没有操作数据库 //TODO:考虑用反射得到sql语句 public class InsertUtil { /// /// 获取插入数据的Sql语句。以换行结尾 /// public static string GetInsertSql(string tableName, string[] columnsNames, string[] values) { if (columnsNames == null || columnsNames.Length == 0 || values == null || values.Length == 0) return string.Empty; if (columnsNames.Length != values.Length) throw new ArgumentException("列的个数与对应的值的个数不等"); return "insert into " + tableName + "(" + string.Join(",", columnsNames) + ") values('" + string.Join("','", values) + "')" + Environment.NewLine; } ///// ///// 插入车辆到数据库中。 ///// 全部成功,返回true;只要有一个失败,则返回false ///// //public static bool InsertCar(IList allNeedInsertCar, out Dictionary failedCars) //{ // failedCars = new Dictionary(); // bool successResult = true; // using (SqlHelperEx she = new SqlHelperEx()) { // foreach (var car in allNeedInsertCar) { // string insertSql = GetInsertSql(TableName.车辆表, new string[] { "Car_ID", "Car_Name" }, new string[] { car.Car_ID.ToString(), car.Car_Name }); // if (string.IsNullOrEmpty(insertSql)) // continue; // bool success; // she.ExecuteNonQuery(insertSql, out success); // if (!success) { // failedCars.Add(car.Car_ID, car.Car_Name); // successResult = false; // continue; // } // } // } // return successResult; //} } }