How to change DataType into coresponding MySqlType ??

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
AceEmbler
Posts: 26
Joined: Fri 04 Aug 2006 09:45

How to change DataType into coresponding MySqlType ??

Post by AceEmbler » Wed 04 Oct 2006 11:16

I have got DataTable.Column[].DataType how to change it into MySqlType.

I need it for parameters in MysqlDataTable.UpdateCommand.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Wed 04 Oct 2006 12:36

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);

AceEmbler
Posts: 26
Joined: Fri 04 Aug 2006 09:45

Post by AceEmbler » Thu 05 Oct 2006 08:31

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.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Thu 05 Oct 2006 09:00

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.

AceEmbler
Posts: 26
Joined: Fri 04 Aug 2006 09:45

Post by AceEmbler » Thu 05 Oct 2006 09:12

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.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Thu 05 Oct 2006 09:26

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();

AceEmbler
Posts: 26
Joined: Fri 04 Aug 2006 09:45

Post by AceEmbler » Thu 05 Oct 2006 10:02

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(); //??

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Thu 05 Oct 2006 10:30

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();

Post Reply