如何在.NET中创建通用SQL参数?
根据Microsoft在上的文档,.NET Framework数据提供程序以不同的方式处理命名和指定参数以及参数占位符如何在.NET中创建通用SQL参数?,.net,sql,ado.net,oracleclient,.net,Sql,Ado.net,Oracleclient,根据Microsoft在上的文档,.NET Framework数据提供程序以不同的方式处理命名和指定参数以及参数占位符 System.Data.SqlClient使用格式为@parametername System.Data.OleDb和System.Data.Odbc使用问号(?)表示的位置参数标记 System.Data.OracleClient使用格式为:parmname(或parmname)的命名参数 我正在编写返回SQL的方法,这些SQL将用于参数化语句。如果我使用标准SQL,这些
- System.Data.SqlClient使用格式为
@parametername
- System.Data.OleDb和System.Data.Odbc使用问号(
)表示的位置参数标记?
- System.Data.OracleClient使用格式为
(或parmname)的命名参数:parmname
我正在编写返回SQL的方法,这些SQL将用于参数化语句。如果我使用标准SQL,这些语句应该可以移植到各种数据库中。如何创建通常有效的参数,而不将数据提供程序的问题泄漏到SQL组件?只需避免@和使用System.data.SqlClient/System.data.OracleClient即可 顺便说一句,如果您希望它是可移植的,请使用System.Data.IDbCommand或System.Data.Common.DbCommand 使用接口还是抽象类取决于您的品味。
接口更合适,但是抽象类有一些在接口中不可用的附加方法(例如DataReader.HasRows)。 我使用了这个界面,但回想起来,这是一个错误。我应该使用抽象类(System.Data.Common.DbAnything而不是System.Data.IAnything) 您可以向这些对象写入扩展方法“AddParameter”,也可以将DAL转换为抽象类,并添加方法AddParameter,在其中可以覆盖respective实例的变量名
public abstract class cDAL
{
// From Type to DBType
protected virtual System.Data.DbType GetDbType(Type type)
{
// http://social.msdn.microsoft.com/Forums/en/winforms/thread/c6f3ab91-2198-402a-9a18-66ce442333a6
string strTypeName = type.Name;
System.Data.DbType DBtype = System.Data.DbType.String; // default value
try
{
if (object.ReferenceEquals(type, typeof(System.DBNull)))
{
return DBtype;
}
if (object.ReferenceEquals(type, typeof(System.Byte[])))
{
return System.Data.DbType.Binary;
}
DBtype = (System.Data.DbType)Enum.Parse(typeof(System.Data.DbType), strTypeName, true);
// Es ist keine Zuordnung von DbType UInt64 zu einem bekannten SqlDbType vorhanden.
// http://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.71).aspx
if (DBtype == System.Data.DbType.UInt64)
DBtype = System.Data.DbType.Int64;
}
catch (Exception)
{
// add error handling to suit your taste
}
return DBtype;
} // End Function GetDbType
public virtual System.Data.IDbDataParameter AddParameter(System.Data.IDbCommand command, string strParameterName, object objValue)
{
return AddParameter(command, strParameterName, objValue, System.Data.ParameterDirection.Input);
} // End Function AddParameter
public virtual System.Data.IDbDataParameter AddParameter(System.Data.IDbCommand command, string strParameterName, object objValue, System.Data.ParameterDirection pad)
{
if (objValue == null)
{
//throw new ArgumentNullException("objValue");
objValue = System.DBNull.Value;
} // End if (objValue == null)
System.Type tDataType = objValue.GetType();
System.Data.DbType dbType = GetDbType(tDataType);
return AddParameter(command, strParameterName, objValue, pad, dbType);
} // End Function AddParameter
public virtual System.Data.IDbDataParameter AddParameter(System.Data.IDbCommand command, string strParameterName, object objValue, System.Data.ParameterDirection pad, System.Data.DbType dbType)
{
System.Data.IDbDataParameter parameter = command.CreateParameter();
if (!strParameterName.StartsWith("@"))
{
strParameterName = "@" + strParameterName;
} // End if (!strParameterName.StartsWith("@"))
parameter.ParameterName = strParameterName;
parameter.DbType = dbType;
parameter.Direction = pad;
// Es ist keine Zuordnung von DbType UInt64 zu einem bekannten SqlDbType vorhanden.
// No association DbType UInt64 to a known SqlDbType
if (objValue == null)
parameter.Value = System.DBNull.Value;
else
parameter.Value = objValue;
command.Parameters.Add(parameter);
return parameter;
} // End Function AddParameter
public virtual T GetParameterValue<T>(System.Data.IDbCommand idbc, string strParameterName)
{
if (!strParameterName.StartsWith("@"))
{
strParameterName = "@" + strParameterName;
}
return InlineTypeAssignHelper<T>(((System.Data.IDbDataParameter)idbc.Parameters[strParameterName]).Value);
} // End Function GetParameterValue<T>
}
您可以通过读取连接字符串web.config条目中的provider
来创建相应的类实例
PS:System.Data.OracleClient不推荐使用,请使用ODP.NET(您仍然需要Oracle安装的本机OracleClient dll[免费下载])我认为它们没有太大区别。OleDb可以识别
@字段
。这是一种普遍现象。甲骨文也应该会接受的。现在,在向command对象添加参数时,可以完全避免使用@field
前缀,只需添加field
。PostGre也可以使用@field。
public class cOleDb : cDAL
{
public overrideSystem.Data.IDbDataParameter AddParameter(System.Data.IDbCommand command, string strParameterName, object objValue)
{
strParameterName = "?";
return AddParameter(command, strParameterName, objValue, System.Data.ParameterDirection.Input);
} // End Function AddParameter
// Etc.
}