Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CommandBehavior.SchemaOnly
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
CommandBehavior.SchemaOnly
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01579375
Message ID:
01579375
Views:
102
Hi everybody,

I found some code online and tested this code in the LinqPad:
string strConn, strSQL;
  strConn = @"Data Source=(local);Initial Catalog=SiriusSQL;" +
             "Trusted_Connection=Yes;";
  SqlConnection cn = new SqlConnection(strConn);
  cn.Open();
  strSQL = "SELECT * FROM dbo.items";
  SqlCommand cmd = new SqlCommand(strSQL, cn);
  SqlDataReader rdr;
  rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly);
  DataTable tbl = rdr.GetSchemaTable();
  
    foreach (DataRow r in tbl.Rows)
        {
          Console.WriteLine("------------------------------------------------------------------");
          foreach (DataColumn dc in tbl.Columns )
          {
            Console.WriteLine("Property: {0, -30} Value: {1}", dc.ColumnName, r[dc].ToString());
          }
        }
  rdr.Close();
  cn.Close();
Now, I defined this class:
/// <summary>
   /// Column schema - some properties
   /// </summary>
    public class ColumnSchema
    {
       public String ColumnName { get; set; }
       public Type DataType { get; set; }
       public SqlDbType DbType { get; set; }
       public Int16 ColumnSize { get; set; }
       public Int16 Precision { get; set; }
       public Int16 Scale { get; set; }
    }
And so I have this code:
 using (SqlDataReader sqlDataReader = command.ExecuteReader(CommandBehavior.SchemaOnly))
                  {
                     DataTable tbl = sqlDataReader.GetSchemaTable();
                     foreach (DataRow r in tbl.Rows)
                     {
                        ColumnSchema colSchema = new ColumnSchema();
                        String readerColumnName = r["ColumnName"].ToString();
                        colSchema.ColumnName = readerColumnName;
                        colSchema.ColumnSize = r.Field<Int16>("ColumnSize");
                        colSchema.DataType = (Type)(r["DataType"].ToString());
The last line is failing, of course.

I am not sure how to get that DataType property to be the Type type. In the code above when I output all info, that was fine, as I output everything as a string. But how do I know the actual type of returned property and how can I set it for my new ColumnSchema properties?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform