Fri 7 Jul 2006
MySQL Connector/.NET field names
Posted by NZEYIMANA Emery Fabrice under C# , MySQL , Software , Visual BasicComments Off
While using Connector/.NET to connect to MySQL I prefer accessing the DataReader fields using their names.
Like
[code lang="C#"]
string sCustomerName = dbReader.GetString("CustomerName") ;
int nSomeIntegerVariable = dbReader.GetInt32("AnIntegerField"); [/code]
I love doing it this way because I can reorder my columns without having to change the access code.
With the current connector, one should retreive the field Index (using the GetOrdinal() Method) and pass that ordinal (Index) to one of the accessors to get the value of the column.
I have been doing that but I found it cumbersome and decided to add some methods in the DataReader Class definition.
The methods I added are:
[code lang="C#"]
public bool GetBoolean(String name)
public byte GetByte(String name)
public long GetBytes(String name, long dataIndex, byte[] buffer, int bufferIndex, int length)
public char GetChar(String name)
public long GetChars(String name, long fieldOffset, char[] buffer, int bufferoffset, int length)
public String GetDataTypeName(String name)
public MySqlDateTime GetMySqlDateTime(String name)
public DateTime GetDateTime(String name)
public Decimal GetDecimal(String name)
public double GetDouble(String name)
public Type GetFieldType(String name)
public float GetFloat(String name)
public Guid GetGuid(String name)
public Int16 GetInt16(String name)
public Int32 GetInt32(String name)
public Int64 GetInt64(String name)
public String GetString(String name)
public TimeSpan GetTimeSpan(String name)
public object GetValue(String name)
public UInt16 GetUInt16(String name)
public UInt32 GetUInt32(String name)
public UInt64 GetUInt64(String name)
public bool IsDBNull(String name)[/code]
In the XML comments of each of these methods I added a line saying that
ordinal based lookups are faster.
The modified file can be accessed at
/dev/mysql/net/mysqlclient/datareader.cs
For those who won’t re-build the connector themselves I have also uploadead
a binary at
/dev/mysql/net/bin/net-2.0/Release/MySql.Data.dll