Page 1 of 1

How to change DataType into coresponding MySqlType ??

Posted: Wed 04 Oct 2006 11:16
by AceEmbler
I have got DataTable.Column[].DataType how to change it into MySqlType.

I need it for parameters in MysqlDataTable.UpdateCommand.

Posted: Wed 04 Oct 2006 12:36
by Alexey
Please describe in detail what exactly you need or what is the problem.
Take a look at the code below:

Code: Select all

mySqlDataTable1.SelectCommand.Parameters.Add("paramName", MySqlType.Int);

Posted: Thu 05 Oct 2006 08:31
by AceEmbler
Alexey wrote:Please describe in detail what exactly you need or what is the problem.
Take a look at the code below:

Code: Select all

mySqlDataTable1.SelectCommand.Parameters.Add("paramName", MySqlType.Int);
As I have mentioned before I have filled DataTable with data from database, but I dont know of what type specified column is. But I can get Column data Type like that DataTable.Column[].DataType , but it is not MysqlType which is needed in Parameters but Type. How can I know what MysqlType corespod to which Type.

Posted: Thu 05 Oct 2006 09:00
by Alexey
If you need to know colunms' data types of any table, create a MySqlDataAdapter, configure it with select command like

Code: Select all

select * from 
Press Preview button and then press Schema Tables button.

Posted: Thu 05 Oct 2006 09:12
by AceEmbler
Alexey wrote:If you need to know colunms' data types of any table, create a MySqlDataAdapter, configure it with select command like

Code: Select all

select * from 
Press Preview button and then press Schema Tables button.
I need to know it at run time.

Posted: Thu 05 Oct 2006 09:26
by Alexey
Then use the following code:

Code: Select all

      MySqlConnection conn = new MySqlConnection("");
      MySqlCommand cmd = new MySqlCommand("select * from ", conn);
      conn.Open();
      MySqlDataReader reader = cmd.ExecuteReader();
      DataTable dt = reader.GetSchemaTable();

Posted: Thu 05 Oct 2006 10:02
by AceEmbler
Alexey wrote:Then use the following code:

Code: Select all

      MySqlConnection conn = new MySqlConnection("");
      MySqlCommand cmd = new MySqlCommand("select * from ", conn);
      conn.Open();
      MySqlDataReader reader = cmd.ExecuteReader();
      DataTable dt = reader.GetSchemaTable();
And how am I suposed to use this column data type obtained by your sniplet in:
UpdateCommand.Parameters.Add(); //??

Posted: Thu 05 Oct 2006 10:30
by Alexey
OK, use GetSchema() method of MySqlConnection component:

Code: Select all

      MySqlConnection conn = new MySqlConnection("");  
      MySqlCommand cmd = new MySqlCommand("select * from  where =:", conn); 
      DataTable dt = conn.GetSchema("Columns", new string[] { "", "", "%" });
      cmd.Parameters.Add("", dt.Rows[].ItemArray[dt.Columns["DataType"].Ordinal].ToString());
      cmd.Parameters[""].Value = ;
      MySqlDataReader reader = cmd.ExecuteReader();