gpt4 book ai didi

C#:映射 IDataReader.GetTableSchema() 和 DbType 枚举?

转载 作者:搜寻专家 更新时间:2023-10-30 20:31:15 29 4
gpt4 key购买 nike

所以,我有以下内容:"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.DecimalDbType.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希望这有助于更好地澄清。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

关于C#:映射 IDataReader.GetTableSchema() 和 DbType 枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6344326/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com