1

我声明了一个新的数据集。然后使用以下命令向其添加数据表

DataTable newtable = ds.Tables.Add("returnresult");

现在我试图在这个数据表中包含两列,数据类型为“nvarchar”。但是通常的命令不接受 nvarchar 作为数据类型

 newtable.Columns.Add("UniqueID",typeof());

使用此命令不会在数据类型中显示 nvarchar。是否有任何其他命令可以将 nvarchar 作为列的数据类型?

4

3 回答 3

7

类型nvarchar是数据库类型,DataTable是内存对象类型nvarchar/varcharstring添加列时的默认类型。

所以这会创建一个DataColumn类型并将其添加string到表中:

newtable.Columns.Add("UniqueID");  // or:
newtable.Columns.Add("UniqueID", typeof(string));

如果要指定文本列的最大长度,请使用它的MaxLength属性。

但是如果你从数据库中填充DataTable,你应该使用 DataAdapter.Fill它自动推断数据库类型的类型和长度。

DataType属性支持以下基本 .NET Framework 数据类型:

Boolean
Byte
Char
DateTime
Decimal
Double
Guid
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64
Byte[]
于 2013-09-21T20:52:30.983 回答
1

在 .NETstring中,等价于NVARCHAR(Any Length),VARCHAR(Any Length)等...

尝试:newtable.Columns.Add("UniqueID",typeof(string));

于 2013-09-21T20:52:26.227 回答
0
        DataTable newtable = ds.Tables.Add("returnresult");
        DataColumn newcolumn = newtable.Columns.Add("UniqueID", typeof(string));
        //newcolumn.MaxLength = n;   // nchar(n)
        newcolumn.MaxLength = 536.870.912; //nvarchar(max)  Variable-length Unicode data. Max 536.870.912 chars.
于 2013-09-21T20:55:30.740 回答