How to get column size from database in .NET?
+3
A:
I'm using code like the following:
using System.Data.SqlClient;
string cmdText = string.Format("SELECT * FROM {0} WHERE 1 = 0", tableName);
SqlCommand sqlCommand = new SqlCommand(cmdText, m_sqlConnection);
using (OrmReader ormReader = new OrmReader(sqlCommand))
{
DataTable dataTable = ormReader.sqlDataReader.GetSchemaTable();
foreach (DataRow dataRow in dataTable.Rows)
{
string columnName = (string)dataRow["ColumnName"];
Type dataType = (Type)dataRow["DataType"];
int columnSize = (int)dataRow["ColumnSize"];
bool allowDBNull = (bool)dataRow["AllowDBNull"];
bool isIdentity = (bool)dataRow["IsIdentity"];
bool isAutoIncrement = (bool)dataRow["IsAutoIncrement"];
string dataTypeName = (string)dataRow["DataTypeName"];
object providerType = dataRow["ProviderType"];
//CAUTION: don't know how to get the field's default value from the DB
string defaultValue = null;
... etc ...
}
}
ChrisW
2009-10-05 06:04:20
+3
A:
If you're using SQL Server 2005 and up, you could try something like this query here:
SELECT
c.name, t.name, c.max_length
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
WHERE
c.object_id = OBJECT_ID('YourTableNameHere')
This will give you the column name, type name and maximum length of the columns of that table.
Marc
marc_s
2009-10-05 06:08:17
A:
Sorry!!!
i am actually working on WPF and i ma using sql server 2008, and i want to put my textbox size as same as respective column size, so i need the max length of a column, how to do it in xaml.cs file?
Jolley
2009-10-05 09:00:10