I would like to pass poco properties to an stored procedure (update and add the Object) With earlier versions of Enterprise Library (e.g. v2.0) I can do something like this:
我希望将poco属性传递到一个存储过程(更新和添加对象),并使用企业库的早期版本(例如v2.0),我可以这样做:
var arrParam = SqlHelperParameterCache.GetSpParameterSet(ConnectionString(),
SprocNameSet);
for (int x = 0; x <= arrParam.Length - 1; x++)
{
System.Reflection.PropertyInfo pi =
dataObject.GetType()
.GetProperty(arrParam[x].ParameterName
.Substring(1, Convert.ToInt32(arrParam[x].ParameterName.Length) - 1));
arrParam[x].Value = pi.GetValue(myDataObject, null);
}
SqlHelper.ExecuteScalar(ConnectionString(),
CommandType.StoredProcedure,
SprocNameSet,
arrParam);
But with Version 5.0 (maybe earlier?) the SqlHelperParameterCache.GetSpParameterSet method is gone.
但是对于版本5.0(可能更早),SqlHelperParameterCache。GetSpParameterSet方法了。
The question is: how can I get the stored-proc-Parameters and fill these with the poco-properties-values?
问题是:我如何获得存储-proc参数,并将这些参数填充到poco-properties-values中?
1 个解决方案
#1
0
You can do something like this:
你可以这样做:
Database db = DatabaseFactory.CreateDatabase();
string spName = "MySP";
var parameters = new object[] { "Information", 22 };
int value = (int)db.ExecuteScalar(spName, parameters);
Now, this relies on the parameter order. If you want to use names and auto populate the DbCommand and your database supports parameter discovery (e.g. SQL Server) then you could do something like:
现在,这依赖于参数顺序。如果您想要使用名称和自动填充DbCommand,并且您的数据库支持参数发现(例如SQL Server),那么您可以这样做:
public class MyClass
{
public string Severity { get; set; }
public int OtherValue { get; set; }
}
MyClass myClass = new MyClass() { OtherValue = 1, Severity = "Information" };
Database db = DatabaseFactory.CreateDatabase();
string spName = "MySP";
DbCommand cmd = db.GetStoredProcCommand(spName);
db.PopulateCommandValues(cmd, myClass);
int value = (int)db.ExecuteScalar(cmd);
public static class DatabaseExtensions
{
public static void PopulateCommandValues<T>(this Database db,
DbCommand cmd, T poco)
{
if (!db.SupportsParemeterDiscovery)
{
throw new InvalidOperationException("Database does not support parameter discovery");
}
db.DiscoverParameters(cmd);
foreach (DbParameter parameter in cmd.Parameters)
{
if (parameter.Direction != System.Data.ParameterDirection.Output &&
parameter.Direction != System.Data.ParameterDirection.ReturnValue)
{
PropertyInfo pi = poco.GetType().GetProperty(
parameter.ParameterName.Substring(1)); // remove @ from parameter
if (pi != null)
{
parameter.Value = pi.GetValue(poco, null);
}
}
}
}
}
This assumes that the POCO property names are the same as the stored procedure parameter names.
这假设POCO属性名称与存储过程参数名称相同。
#1
0
You can do something like this:
你可以这样做:
Database db = DatabaseFactory.CreateDatabase();
string spName = "MySP";
var parameters = new object[] { "Information", 22 };
int value = (int)db.ExecuteScalar(spName, parameters);
Now, this relies on the parameter order. If you want to use names and auto populate the DbCommand and your database supports parameter discovery (e.g. SQL Server) then you could do something like:
现在,这依赖于参数顺序。如果您想要使用名称和自动填充DbCommand,并且您的数据库支持参数发现(例如SQL Server),那么您可以这样做:
public class MyClass
{
public string Severity { get; set; }
public int OtherValue { get; set; }
}
MyClass myClass = new MyClass() { OtherValue = 1, Severity = "Information" };
Database db = DatabaseFactory.CreateDatabase();
string spName = "MySP";
DbCommand cmd = db.GetStoredProcCommand(spName);
db.PopulateCommandValues(cmd, myClass);
int value = (int)db.ExecuteScalar(cmd);
public static class DatabaseExtensions
{
public static void PopulateCommandValues<T>(this Database db,
DbCommand cmd, T poco)
{
if (!db.SupportsParemeterDiscovery)
{
throw new InvalidOperationException("Database does not support parameter discovery");
}
db.DiscoverParameters(cmd);
foreach (DbParameter parameter in cmd.Parameters)
{
if (parameter.Direction != System.Data.ParameterDirection.Output &&
parameter.Direction != System.Data.ParameterDirection.ReturnValue)
{
PropertyInfo pi = poco.GetType().GetProperty(
parameter.ParameterName.Substring(1)); // remove @ from parameter
if (pi != null)
{
parameter.Value = pi.GetValue(poco, null);
}
}
}
}
}
This assumes that the POCO property names are the same as the stored procedure parameter names.
这假设POCO属性名称与存储过程参数名称相同。