我试图从MySQL数据库中获取表的主键。当我使用MySQL ODBC 3.51驱动程序时,下面的代码工作正常。当我更改代码以使用MySQL ODBC 5.2 ANSI驱动程序时,它不起作用。我也尝试过MySQL ODBC 5.1驱动程序。任何人都知道为什么我无法使用最新版本的MySQL ODBC驱动程序获取表的主键?
此代码可以工作并返回DataTable中的PrimaryKey:
using (OdbcConnection connection = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=IP_Address;Database=DB_Name;User=username;Password=password;Option=3;"))
{
connection.Open();
DataTable dt = new DataTable();
using(OdbcCommand command = connection.CreateCommand())
{
command.CommandText = "SET SESSION sql_mode = 'ANSI';";
command.ExecuteNonQuery();
command.CommandText = "SELECT * FROM \"items\"";
using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
{
adapter.FillSchema(dt,SchemaType.Source);
}
}
connection.Close();
}
此代码不起作用 - DataTable中没有返回PrimaryKey:
using (OdbcConnection connection = new OdbcConnection("Driver={MySQL ODBC 5.2 ANSI Driver};Server=IP_Address;Database=DB_Name;User=username;Password=password;Option=3;"))
{
connection.Open();
DataTable dt = new DataTable();
using(OdbcCommand command = connection.CreateCommand())
{
command.CommandText = "SET SESSION sql_mode = 'ANSI';";
command.ExecuteNonQuery();
command.CommandText = "SELECT * FROM \"items\"";
using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
{
adapter.FillSchema(dt,SchemaType.Source);
}
}
connection.Close();
}