public class Customer : BaseClass<Customer>
{
public string Name { get; set; }
public DateTime? DateOfBirth { get; set; }
public string TelephoneNumber { get; set; }
public string InsuranceProvider { get; set; }
public int? PolicyNumber { get; set; }
public byte [] Photo { get; set; }
}
此消息显示:
或这个:
如何修改此代码以能够持久化数据?
CustomerDAO.cs
class CustomerDAO
{
.....
public int Save(ITransactionManager tm, Customer item)
{
int count = -1;
try
{
ISqlQueryExecutor<Customer> queryExecutor = new SqlQueryExecutor<Customer>(tm);
count =
queryExecutor.
ExecuteNonQuery(@"INSERT INTO Customer(
ID
,Name
,TelephoneNumber
,DateOfBirth,
InsuranceProvider,
PolicyNumber)
VALUES(
@ID
,@Name
,@TelephoneNumber
,@DateOfBirth,
@InsuranceProvider,
@PolicyNumber)",
item.ID,
item.Name,
item.TelephoneNumber,
item.DateOfBirth,
item.InsuranceProvider,
item.PolicyNumber,item.Photo
);
//new DbParameter(item.ID, DbType.Int32),
//new DbParameter(item.Name, DbType.String),
//new DbParameter(item.TelephoneNumber, DbType.String),
//new DbParameter(item.DateOfBirth, DbType.DateTime),
//new DbParameter(item.InsuranceProvider, DbType.String),
//new DbParameter(item.PolicyNumber, DbType.Int32)
//new DbParameter(item.Photo, DbType.Binary)
//);
string str = string.Empty;
}
catch (Exception ex)
{
throw ex;
}
return count;
}
.... ....
}
客户BLL.cs
class CustomerBLL
{
... ... ...
public int Save(Customer item)
{
int newId = 0;
ITransactionManager tm = ApplicationContext.Get(DBNameConst.ActiveConnStringName);
try
{
tm.BeginTransaction();
item.ID = newId = PivotTable.GetNextID(tm, "Customer").Value;
customerDao.Save(tm, item);
PivotTable.UpdateNextIdField(tm, "Customer", newId);
tm.CommitTransaction();
}
catch (Exception ex)
{
tm.RollbackTransaction();
throw ex;
}
return newId;
}
... ... ...
}
ASqlQueryExecutor.cs
public abstract class ASqlQueryExecutor<T> : ISqlQueryExecutor<T>
{
public virtual int ExecuteNonQuery(string queryString, params object[] parameters)
{
int count = -1;
try
{
Command = ParameterAttacher.AttachSaveParameters(TransactionManager, queryString, parameters);
Command.CommandText = queryString;
count = Command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
return count;
}
参数附件.cs
class ParameterAttacher
{
public static IDbCommand AttachSaveParameters(ITransactionManager tm, string queryString, params object [] argumentsList)
{
IDbCommand command = new DbObjectInstantiator(tm.ProviderName).CreateCommand();
command.Connection = tm.Connection;
command.Transaction = tm.Transaction;
IList<string> parameterNamesList = new List<string>(ParameterParser.Parse(queryString));
if (parameterNamesList.Count > 0 && argumentsList.Length == argumentsList.Length)
{
int i = 0;
foreach (string paramName in parameterNamesList)
{
Attach(command, paramName, argumentsList[i]);
++i;
}
}
return command;
}
public static void Attach(IDbCommand command, string paramName, object dbParam)
{
IDbDataParameter param = command.CreateParameter();
param.ParameterName = paramName;
param.Value = (dbParam==null) ? ((object)DBNull.Value) : dbParam;
//param.DbType = dbParam.DbType;
command.Parameters.Add(param);
}
}