Search code examples
mysqlvb.netvisual-studio-2008zerofill

Detecting zerofill in MySQL field with VS 2008 VB


Using .NET connector: http://dev.mysql.com/doc/refman/5.1/en/connector-net-ref.html

A field is set in MySQL as:

decimal(6) zerofill not null

How can the "zerofill" flag be detected in VB?


Solution

  • You can obtain column information using SHOW COLUMNS IN tablename, this query returns something in the form:

    field         type                          null   key   default    extra
    fieldname     int(10) unsigned zerofill      no            0    
    

    So to obtain the flag zerofill you have to look in type if the string returned in int contains the 'zerofill' string:

    Something like this will do the trick:

    static void Main(string[] args)
    {
        dim conn as MySqlConnection
        conn = new MySqlConnection
        conn.ConnectionString = "Server = yourserver; Database = yourdb; Uid = youruser;Pwd = yourpassword;"
    
        conn.Open
    
        MySqlCommand cmd = new MySqlCommand
        cmd.Connection = conn
        cmd.CommandText = "SHOW COLUMNS IN yourtable"
        cmd.CommandType = CommandType.Text
    
        dim reader as MySqlDataReader
        reader = cmd.ExecuteReader
    
        while reader.Read
            Console.WriteLine("Field:{0}, zerofill:{1}",reader("field"),if(reader("type").ToString().Contains("zerofill"),true,false))
        end while
        conn.Close()
    }