简体   繁体   English

向数据表数据类型nvarchar添加列

[英]adding columns to datatable datatype nvarchar

I declared a new dataset. 我声明了一个新的数据集。 Then added a datatable to it using the following command 然后使用以下命令向其添加数据表

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

now am trying to include two columns in this datatable with datatype "nvarchar". 现在尝试在此数据表中包含数据类型为“ nvarchar”的两列。 But the usual command for this does not accept nvarchar as a datatype 但是通常的命令不接受nvarchar作为数据类型

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

using this command does not show nvarchar in the datatypes. 使用此命令不会在数据类型中显示nvarchar。 is there any other command to get nvarchar as the datatype for columns? 是否还有其他命令将nvarchar用作列的数据类型?

The type nvarchar is a database type, the DataTable is an in-memory objectThe type nvarchar / varchar is string which is the default type if you add a column. 类型nvarchar是一个数据库类型, DataTable是在内存中的objectThe类型nvarchar / varcharstring这是默认的类型,如果你添加一列。

So this creates and adds a DataColumn of type string to the table: 因此,这将创建string类型的DataColumn并将其添加到表中:

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

If you want to specify the maximum length of a text column use it's MaxLength property. 如果要指定文本列的最大长度,请使用其MaxLength属性。

But if you fill a DataTable from database you should use DataAdapter.Fill which automaticially infers the type and length from the database type. 但是,如果从数据库填充DataTable ,则应使用DataAdapter.Fill ,它会自动从数据库类型推断类型和长度。

The DataType property supports the following base .NET Framework data types: DataType属性支持以下基本.NET Framework数据类型:

Boolean
Byte
Char
DateTime
Decimal
Double
Guid
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64
Byte[]

In .NET, string is the equivalent of NVARCHAR(Any Length) , VARCHAR(Any Length) , etc... 在.NET中, string等效于NVARCHAR(Any Length)VARCHAR(Any Length)等。

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

        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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM