使用ODBConction(System.Data.Odbc)在C#中获取MySql表主键
我试图使用C-Sharp(C#)检索MySQL数据库中表的主键,但遇到了问题 我查看了提供的各种元数据集合和相应的列,但是没有一个提供主键。“表”和“索引”集合似乎是最有希望的。有趣的是,OdbcConnection.GetSchema()有一个PrimaryKey属性/方法,但是PrimaryKey属性不会产生null以外的结果 索引和表看起来确实是显而易见的选择。是的,数据库中的表有一个主键,数据库可以工作 这里有一些代码,不过对于这个问题似乎没有必要。为了本示例的目的,我选择了“表”,但可以简单地更改为“索引”(或其他任何内容)。显然,表的列名称是存在的。我只是在那里玩使用ODBConction(System.Data.Odbc)在C#中获取MySql表主键,c#,mysql,primary-key,C#,Mysql,Primary Key,我试图使用C-Sharp(C#)检索MySQL数据库中表的主键,但遇到了问题 我查看了提供的各种元数据集合和相应的列,但是没有一个提供主键。“表”和“索引”集合似乎是最有希望的。有趣的是,OdbcConnection.GetSchema()有一个PrimaryKey属性/方法,但是PrimaryKey属性不会产生null以外的结果 索引和表看起来确实是显而易见的选择。是的,数据库中的表有一个主键,数据库可以工作 这里有一些代码,不过对于这个问题似乎没有必要。为了本示例的目的,我选择了“表”,但可
public String GetPrimaryKey(String strTable)
{
try
{
String strPrimaryKey = null;
String[] strRestricted = new String[4] { null, null, strTable, null };
DataTable oSchema = null;
// Make sure that there is a connection.
if (ConnectionState.Open != this.m_oConnection.State)
this.m_oConnection.Open();
// DATABASE: Get the schema
oSchema = this.m_oConnection.GetSchema("Tables", strRestricted);
// Extract the information related to the primary column, in the format "{System.Data.DataColumn[0]}"
DataColumn[] oPrimaryKeys = oSchema.PrimaryKey;
// Extract: Column Names
foreach (DataRow oRow in oSchema.Rows)
{
// Get the column name.
String strColumnName = oRow["COLUMN_NAME"].ToString();
}
return strPrimaryKey;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
在做研究的过程中,我发现有趣的是,我找不到任何使用GetSchema().PrimaryKey属性的人的帖子
那么我如何识别主键呢
提前谢谢。你的评论是一把神奇的钥匙。我不知道旧接口已被弃用。找到正确的代码有点困难,因为在Indexes集合中没有“COLUMN_NAME”,在Columns集合中也没有“PRIMRY”,所以我不得不经历两次,但新版本要好得多
public String GetPrimaryKey(String strTable)
{
try
{
Boolean bIsPrimary = false;
String strIndexName = null;
String strColumnName = null;
String[] strRestricted = new String[4] { null, null, strTable, null };
DataTable oSchemaIndexes = null;
DataTable oSchemaIndexColumns = null;
// Make sure that there is a connection.
if (ConnectionState.Open != this.m_oConnection.State)
this.m_oConnection.Open();
// DATABASE: Get the schemas needed.
oSchemaIndexes = this.m_oConnection.GetSchema("Indexes", strRestricted);
oSchemaIndexColumns = this.m_oConnection.GetSchema("IndexColumns", strRestricted);
// Get the index name for the primary key.
foreach (DataRow oRow in oSchemaIndexes.Rows)
{
// If we have a primary key, then we found what we want.
strIndexName = oRow["INDEX_NAME"].ToString();
bIsPrimary = (Boolean)oRow["PRIMARY"];
if (true == bIsPrimary)
break;
}
// If no primary index, bail.
if (false == bIsPrimary)
return null;
// Get the corresponding column name.
foreach (DataRow oRow in oSchemaIndexColumns.Rows)
{
// Get the column name.
if (strIndexName == (String)oRow["INDEX_NAME"])
{
strColumnName = (String)oRow["COLUMN_NAME"];
break;
}
}
return strColumnName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
可以说,因为Odbc已被弃用,所以请使用MySqlClient和GetSchema(),并对每个表进行过程调用。你的评论是一把神奇的钥匙。我不知道旧接口已被弃用。找到正确的代码有点困难,因为在Indexes集合中没有“COLUMN_NAME”,在Columns集合中也没有“PRIMRY”,所以我不得不重复两次,但新版本要好得多。您可以回答自己的问题,作为其他人的参考。