c# - convert string value to SqlDbType -


I am creating in-memory data dictionary from these class objects

  public Class DDRO {public string columnname {received; Set; } Type public SqlDB datatype {get; Set; } Public int column size {get; Set; } Receive Public Bull IDCTT { Set; } Public Child AllowDBNull {get; Set; }}  

... in a sub-routine, is populating it with a SqlReader.GetSchema call

  // Column schema to a DataTable Recover in SchemaTable = myReader.GetSchemaTable (); DDRow wrkRow = null; // For each field in the table ... foreach (DataRow myField in schemaTable.Rows) {wrkRow = new DDRow (); WrkRow.ColumnName = Convert ToString (myField ["ColumnName"]); // string wrkRow.DataType = myField ["DataType"]; // SqlDbType  

... except for all the work I think I can not understand the way to convert

  myField ["DataType"]  

To put DDRow.DataType in a SqlDBType value, which is an object,

I can convert it to a string:

< Pre> string S1 = Convert toString (myField ["DataType"]);

but

  SqlParameter p = new SqlParameter (s1, o); SqlDbType q = p.SqlDbType; // & lt; - Error here  

throws an error as the above signal, so the string is not enough.

I think I might be able to run the string value through the conversion table routine SqlDbType value; Is there a short cut?

Edit ------- - In the light of the answer given below Adam, this is the right way to code, and the correct way to test properly (ProviderType in the DDRow definition With DataType replaced)

  wrkRow.ProviderType = (SqlDbType) (int) myField ["ProviderType"]; SqlParameter wrkS = New SqlParameter ("@ ABC", wrkRow.ProviderType, 8); Type SqlDb wrkT = wrkS.SqlDbType; DataType field is the CLR type - SqlDbType does not have the ProviderType field that you are later able to use. are doing. It is an intern that represents the SQL Server data type. You can easily type SqlDb. Below is an example that should be included in your code:  
  var reader = cmd.ExecuteReader (); Var schemaTable = reader.GetSchemaTable (); Var field = new list & lt; DDRow & gt; (); Foreach (DataRow myField in schemaTable.Rows) {fields.Add (New DDRow () {columnname = Convert. ToString (myField ["ColumnName"]), Datatype = (SqlDbType) (int) myField ["ProviderType"]}); }  

Then you can do this to create your SQL Paramet (assuming that your variable is the name of the 'O' column).

  var field = fields first (); SqlParameter p = new SqlParameter ("@" + field.ColumnName, field.DataType); SqlDbType q = p.SqlDbType;  

In your example, it seems that the parameter also has to go through the wrong way. In your class example, the datatype field should be SqlDb type instead of SqlDbType


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -