gpt4 book ai didi

c# - 传递 DataTypeEnum

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:29 27 4
gpt4 key购买 nike

我在尝试调用我正在创建的 C# 项目中的函数时遇到以下错误:

System.Runtime.InteropServices.COMException: Operation is not allowed in this context.
at ADOX.ColumnClass.set_Type(DataTypeEnum pVal)

更新:我现在得到: System.Runtime.InteropServices.COMException (0x80040E3D): 类型无效。

这很奇怪,因为如果我不将任何字符串传递给一个应该返回 DataTypeEnum 的函数,它就可以工作。

我正在调用一个函数并传递一个字符串“widget(in)”以返回 DataTypeEnum 字段类型

static public createTable()
{

ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
table.Name = "Table1";
ADOX.ColumnClass columnHold=new ADOX.ColumnClass();
columnHold.Name="FieldX;
columnHold.Type=setFieldType("widget(in05)"); //this works if nothing passed to function
columnHold.DefinedSize=setFieldSize("widget(in05)"); //This call works
cat.Tables.Append(table);
//more code not listed
}

调用以下函数从单独的数据文件和字段中获取字段类型

static public ADOX.DataTypeEnum setFieldType(string fieldName)
{
//fieldname is from another data file.. skipping the code just to show point
ADOX.DataTypeEnum fieldType= ADOX.DataTypeEnum.adVarChar;
return fieldType;
}

我曾经使用称为宏替换的东西来执行此操作,但由于坏习惯,C# 不允许这样做。

知道如何将类型传递给调用函数吗?谢谢。

最佳答案

ADOX.Field.Column property将列添加到表后是只读的:

This property is read/write until the Column object is appended to a collection or to another object, after which it is read-only.

最简单的解决方案是将列类型指定为 Append method 的一部分:

table.Columns.Append("field1", setFieldType("widget"));

关于c# - 传递 DataTypeEnum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25900156/

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