COL_LENGTH Returns the defined length (in bytes) of a column.Syntax COL_LENGTH ( 'table' , 'column' ) Arguments 'table'Is the name of the table for which to determine column length information. table is an expression of type nvarchar. 'column'Is the name of the column for which to determine length. column is an expression of type nvarchar.Return Types intExamples This example shows the return values for a column of type varchar(40) and a column of type nvarchar(40).USE pubs GO CREATE TABLE t1 (c1 varchar(40), c2 nvarchar(40) ) GO SELECT COL_LENGTH('t1','c1')AS 'VarChar', COL_LENGTH('t1','c2')AS 'NVarChar' GO DROP TABLE t1Here is the result set.VarChar NVarChar 40 80上面是sqlserver 2000的参考 SELECT COL_LENGTH('表名','列名')AS GetCount执行sql后就可以 得到 GetCount 的值 就是列宽了
public bool _AddTableRow(string table_name,Hashtable ht)
{
//建立空结构表
DataSet ds = new DataSet();//
System.Type colType;
string subStr;
byte[] subBytes;
System.Text.StringBuilder sb = new System.Text.StringBuilder("");
int colLen; this.Initi(this.m_Adapter);//
this.Initi(m_Command);//
m_Command.CommandText = "select * from "+table_name+" where 1=2";//
m_Adapter.SelectCommand = m_Command;//
m_Adapter.SelectCommand.Transaction = m_Trans;//
m_Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
m_Adapter.Fill(ds,table_name);//
//建立新行
m_DataRow = ds.Tables[table_name].NewRow();//
System.Collections.IDictionaryEnumerator mycollection = ht.GetEnumerator();//
//遍历循环
while(mycollection.MoveNext())
{
if(mycollection.Value.ToString() == "")
{
m_DataRow[mycollection.Key.ToString()] = DBNull.Value;//
}
else
{
colType = ds.Tables[table_name].Columns[mycollection.Key.ToString()].DataType;
colLen = ds.Tables[table_name].Columns[mycollection.Key.ToString()].MaxLength;
if(colType == System.Type.GetType("System.String") && colLen > 0)
{
subStr = (string)mycollection.Value;//取值
//取数据库中对应长度
colLen = Math.Min(colLen, System.Text.Encoding.Default.GetByteCount(subStr.ToCharArray()));
subBytes = System.Text.Encoding.Default.GetBytes(subStr);
sb.Remove(0, sb.Length);
//截取串
subStr = sb.Append(System.Text.Encoding.Default.GetChars(subBytes, 0, colLen)).ToString();
//subStr.Replace("'", "''");
m_DataRow[mycollection.Key.ToString()] = subStr;
}
else
m_DataRow[mycollection.Key.ToString()] = mycollection.Value; //
}
}
//增加新行
ds.Tables[table_name].Rows.Add(m_DataRow);//
//提交到数据库
m_Builder = new SqlCommandBuilder(m_Adapter);//
this.m_Adapter.Update(ds,table_name);//
return true;
}
Returns the defined length (in bytes) of a column.Syntax
COL_LENGTH ( 'table' , 'column' ) Arguments
'table'Is the name of the table for which to determine column length information. table is an expression of type nvarchar. 'column'Is the name of the column for which to determine length. column is an expression of type nvarchar.Return Types
intExamples
This example shows the return values for a column of type varchar(40) and a column of type nvarchar(40).USE pubs
GO
CREATE TABLE t1
(c1 varchar(40),
c2 nvarchar(40)
)
GO
SELECT COL_LENGTH('t1','c1')AS 'VarChar',
COL_LENGTH('t1','c2')AS 'NVarChar'
GO
DROP TABLE t1Here is the result set.VarChar NVarChar
40 80上面是sqlserver 2000的参考
SELECT COL_LENGTH('表名','列名')AS GetCount执行sql后就可以 得到 GetCount 的值 就是列宽了
大大看看有什么办法,我用的方法:
col.MaxLength(col代表某一列)我把它显示在Labe1中
Label.Text=Convert.ToString (col.MaxLength );
显示出来是-1 不知怎么会事,实际上col最大字符数为50。
但是如果你想得到的是数值型的,那你就不能用这个属性了,你只能得到该列的数据类型,
然后,根据该类型得出该数值类型的长度是什么,
例:
Label1.Text = col.DataType.tostring();
假设得到:System.Int32
再自己写一个简单的二维数组,一维放数据类型,一维放数据类型长度即可根据列的类型得到其长度。
?or use DataAdapter.FillSchema method