Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CommandBehavior.SchemaOnly
Message
 
 
À
29/07/2013 18:27:42
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01579375
Message ID:
01579382
Vues:
41
>>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.
>
>Naomi
>
>What .NET version are you using?
>With my version - 4.5 - the table created by GetSchemaTable has a string column called DataTypeName.
>I don't see any DataType column.
>
>http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable.aspx

I see both DataTypeName and DataType. DataTypeName looks like it returns SqlDbType - may be I can use this property instead of what I use.

I currently have this code
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"]);
                     colSchema.DbType = (SqlDbType)(Convert.ToInt32(r["ProviderType"]));

                     columnTypes.Add(readerColumnName, colSchema);
                  }
which compiles OK but I haven't tested it yet.

It looks like I am using .NET Framework 4

I'll check with my colleagues if we can switch to 4.5
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform