将列添加到datatable数据类型nvarchar

时间:2013-09-21 20:49:46

标签: c# database

我宣布了一个新的数据集。 然后使用以下命令为其添加数据表

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

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

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

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

3 个答案:

答案 0 :(得分: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[]

答案 1 :(得分:1)

在.NET中,string相当于NVARCHAR(Any Length)VARCHAR(Any Length)等等。

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

答案 2 :(得分: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.
相关问题