刚开始时尝试用以下代码来更新DataSet:
Database db
=
DatabaseFactory.CreateDatabase();
string strSql = " Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products " ;
DbCommand command = db.GetSqlStringCommand(strSql);
DataSet ds = db.ExecuteDataSet(command);
DbCommand insertCommand = db.GetStoredProcCommand( " AddProduct " );
db.AddInParameter(insertCommand, " ProductName " , DbType.String, DataRowVersion.Current);
db.AddInParameter(insertCommand, " CategoryID " , DbType.Int32, DataRowVersion.Current);
db.AddInParameter(insertCommand, " UnitPrice " , DbType.Currency, DataRowVersion.Current);
DbCommand deleteCommand = db.GetStoredProcCommand( " DeleteProduct " );
db.AddInParameter(deleteCommand, " ProductID " , DbType.Int32, DataRowVersion.Current);
DbCommand updateCommand = db.GetStoredProcCommand( " UpdateProduct " );
db.AddInParameter(updateCommand, " ProductID " , DbType.Int32, DataRowVersion.Current);
db.AddInParameter(updateCommand, " ProductName " , DbType.String, DataRowVersion.Current);
db.AddInParameter(updateCommand, " LastUpdate " , DbType.DateTime, DataRowVersion.Current);
ds.Tables[ 0 ].Rows.Add( new object [] { DBNull.Value, "Added from program", 13, 26 } );
db.UpdateDataSet(ds, ds.Tables[ 0 ].TableName, insertCommand, updateCommand, deleteCommand, UpdateBehavior.Standard);
string strSql = " Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products " ;
DbCommand command = db.GetSqlStringCommand(strSql);
DataSet ds = db.ExecuteDataSet(command);
DbCommand insertCommand = db.GetStoredProcCommand( " AddProduct " );
db.AddInParameter(insertCommand, " ProductName " , DbType.String, DataRowVersion.Current);
db.AddInParameter(insertCommand, " CategoryID " , DbType.Int32, DataRowVersion.Current);
db.AddInParameter(insertCommand, " UnitPrice " , DbType.Currency, DataRowVersion.Current);
DbCommand deleteCommand = db.GetStoredProcCommand( " DeleteProduct " );
db.AddInParameter(deleteCommand, " ProductID " , DbType.Int32, DataRowVersion.Current);
DbCommand updateCommand = db.GetStoredProcCommand( " UpdateProduct " );
db.AddInParameter(updateCommand, " ProductID " , DbType.Int32, DataRowVersion.Current);
db.AddInParameter(updateCommand, " ProductName " , DbType.String, DataRowVersion.Current);
db.AddInParameter(updateCommand, " LastUpdate " , DbType.DateTime, DataRowVersion.Current);
ds.Tables[ 0 ].Rows.Add( new object [] { DBNull.Value, "Added from program", 13, 26 } );
db.UpdateDataSet(ds, ds.Tables[ 0 ].TableName, insertCommand, updateCommand, deleteCommand, UpdateBehavior.Standard);
但是失败了,后来仔细比较QuickStart中的代码后才发现应该使用Database.AddInParameter(DbCommand command,string name,DbType dbType,string sourceColumn,object value)这个方法,查了一下帮助文档,对比如下:
1.public void AddInParameter(DbCommand command,string name,DbType dbType,object value)
Parameter
command:The command to add the parameter;
name:The name of the parameter;
dbType:One of the DbType values;
values:The value of the parameter;
2.public void AddInParameter(DbCommand command,string name,DbType dbType,string sourceColumn,object value)
Parameter
command:The command to add the parameter;
name:The name of the parameter;
dbType:One of the DbType values;
sourceColumn:The name of the source column mapped to the DataSet and used for loading or returning the value;
values:The value of the parameter;