- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
所以,我有以下内容:"SELECT * FROM MyTable;"
当我执行以下操作时,我得到了 TableData,这很有用,但仍然有一些未知的东西?
//CommandBehavior.KeyInfo seems to actually return the correct primary keys
// not so much with CommandBehavior.SchemaOnly.
IDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo)
DataTable table = reader.GetSchemaTable();
现在,当遍历我的表时,我遇到一个名为“DataType”的列,它是 System.String 或 System.Byte[] 或 System.Int32 等。但是,这只告诉我 .NET 类型例如,System.Decimal
是 DbType.Currency
还是 DbType.Decimal
。因此,当我创建 IDataParameter 时,我不确定要为 DbType 设置什么。
parameter.ColumnName = columnName;
parameter.DbType = DbType.Decimal; (or should it have been Currency?)
基本上,我如何才能获得表的真实架构...或者这有什么关系吗?
最佳答案
如果您正在为存储过程或某些 sql 文本传递参数,则不需要指定参数数据类型。 SqlCommand 将为您正确分配数据类型。
我相信在参数上分配 DBType 的能力是如果你想覆盖系统会为你选择的内容。
使用
SqlCommand.Parameters.AddWithValue("@parameterName", valueAsObject);
命令
编辑您使用的是 IDbCommand,而不是 SqlCommand。我知道SqlCommand和Oracle命令都不需要你指定DbType,但我不知道其他框架是否需要你显式设置DbType。这是一种将 system.type 转换为 DbType 枚举值的方法:
Class DBTypeConversion
{
private static String[,] DBTypeConversionKey = new String[,]
{
{"BigInt","System.Int64"},
{"Binary","System.Byte[]"},
{"Bit","System.Boolean"},
{"Char","System.String"},
{"DateTime","System.DateTime"},
{"Decimal","System.Decimal"},
{"Float","System.Double"},
{"Image","System.Byte[]"},
{"Int","System.Int32"},
{"Money","System.Decimal"},
{"NChar","System.String"},
{"NText","System.String"},
{"NVarChar","System.String"},
{"Real","System.Single"},
{"SmallDateTime","System.DateTime"},
{"SmallInt","System.Int16"},
{"SmallMoney","System.Decimal"},
{"Text","System.String"},
{"Timestamp","System.DateTime"},
{"TinyInt","System.Byte"},
{"UniqueIdentifer","System.Guid"},
{"VarBinary","System.Byte[]"},
{"VarChar","System.String"},
{"Variant","System.Object"}
};
public static SqlDbType SystemTypeToDbType( System.Type sourceType )
{
SqlDbType result;
String SystemType = sourceType.ToString();
String DBType = String.Empty;
int keyCount = DBTypeConversionKey.GetLength(0);
for(int i=0;i<keyCount;i++)
{
if(DBTypeConversionKey[i,1].Equals(SystemType)) DBType = DBTypeConversionKey[i,0];
}
if (DBType==String.Empty) DBType = "Variant";
result = (SqlDbType)Enum.Parse(typeof(SqlDbType), DBType);
return result;
}
public static Type DbTypeToSystemType( SqlDbType sourceType )
{
Type result;
String SystemType = String.Empty;
String DBType = sourceType.ToString();
int keyCount = DBTypeConversionKey.GetLength(0);
for(int i=0;i<keyCount;i++)
{
if(DBTypeConversionKey[i,0].Equals(DBType)) SystemType = DBTypeConversionKey[i,1];
}
if (SystemType==String.Empty) SystemType = "System.Object";
result = Type.GetType(SystemType);
return result;
}
http://social.msdn.microsoft.com/Forums/en/winforms/thread/c6f3ab91-2198-402a-9a18-66ce442333a6希望这有助于更好地澄清。
关于C#:映射 IDataReader.GetTableSchema() 和 DbType 枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6344326/
我有两种情况从IDataReader对象中提取信息 Case - 1 - 长度,计算序数然后解析字符串 public static string GetString(IDataReader rdr,
我正在编写一个类,它封装了使用 ADO.NET 从数据库中检索数据的复杂性。其核心方法是 private void Read(Action action) where T : class, new()
实际上不一定是 IDataReader . 我有一个类似这样的函数: public IEnumerable GetObjects() { IDataReader dr = GetSomeDataR
我有一个看起来像这样的 XML: Row 1 - Col 1 Row 1 - Col 2 Row 1 - Col 3 ...
出于某种原因,这段代码: MethodInfo iDataReaderIndexerProperty = typeof(IDataReader).GetProperties() .Single
我正在尝试创建一个“Helper”类,您只需在其中传递驱动程序和参数,该类就会为您执行连接和连接字符串组装。 我一直在使用 System.Data 的接口(interface),例如 IDataRea
这个问题在这里已经有了答案: Does casting create new object? (3 个答案) 关闭 4 年前。 假设我创建了一个数据读取器。 我的数据库类中有一个方法 - 以这种方式
如何使用泛型将 DataReader 对象映射到类对象? 例如我需要做以下事情: public class Mapper { public static List MapObj
谁能告诉我这两段代码的区别?为什么要使用 IDataReader? using (IDataReader reader = cmd.ExecuteReader()) { while (read
在使用 reader.Read(); 遍历行之前,是否有任何方法可以获取从 SQL 查询(从 IDataReader)返回的总行数? 最佳答案 没有。 IDataReader 是结果集的简单只进 Vi
我有一个 List有一百万个元素。 (它实际上是一个 SubSonic Collection 但它不是从数据库加载的)。 我目前使用 SqlBulkCopy 如下: private string Fa
我继承的典型查询执行模式是这样的: using (IDataReader r = query.ExecuteReader()) { while (r.Read()) { // etc.
我正在考虑在 IDataReader 上模拟一个扩展方法(在项目中使用) . ReadAll()时我想返回一个测试数据集合(下)被调用。 public static IEnumerable ReadA
我正在尝试确定代码库中是否存在明显的错误。 有问题的代码调用第三方 dll,它返回一个 IDataReader。如果代码使用读取器而不处理它,它不会显式返回到池中,对吗? 调用代码如下: IDataR
我正在为 map 实体和数据集使用 AutoMapper AutoMapper.Mapper.CreateMap(): 并且我在 Home 实体中有一个名为 MobileNumber 的属性,并且想在
我正在做一个大规模的插入/更新操作。 所以我正在使用 SqlBulkCopy。 SqlConnection myConnection = new SqlConnection(myConnectionS
我在窗口 C# vs05 上工作。我想通过 IDataReader 读取图像....在 oledb 中....我该怎么做假设我想从数据库列名称学生 ID 中读取 int 值然后我用下面的方式编写代码
我想从数据库中读取数据到列表。 我试过下面的代码 public List StoredProcedureForIList(string spName, params IDataParameter[]
我正在尝试将 SQL 数据库中的数据(1 个表中的 4 列)加载到一个列表中,到目前为止已经完成了 List FNameList = (from IDataRecord r in myReader
在使用 IDataReader 对象(DbDataReader)中包含的信息加载它后,我试图向 DataTable 添加一个新条目> 具体来说。加载顺利,我认为对数据库的查询是正确的。 尝试添加新行时
我是一名优秀的程序员,十分优秀!