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:
01579383
Vues:
43
Wondering if they removed DataType in 4.5 ? This would be most unfortunate - how can I get both .NET type and SqlDbType then using SchemaOnly?

>>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
If it's not broken, fix it until it is.


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

Click here to load this message in the networking platform