C# 中的 MySQL Schema






4.86/5 (11投票s)
2004年1月19日
3分钟阅读

191209

5337
一篇关于如何使用 C# 获取 MySQL 模式的文章。该程序利用 ODBC API。
图 1.0:MySQL 实用程序的屏幕截图。
引言
本文涵盖了如何与 MySQL 数据库通信并从中提取模式。
背景
正如你们许多人所知,MySQL 是一个开源数据库。它可免费用于非商业用途。 本文对于使用 MySQL 和 C# 进行开发的任何人都会有所帮助。 制作此实用程序的动机是什么? 应用程序可能会在数据库活动过程中崩溃。 通常,这些数据库可能不会与您的开发/测试机器位于同一站点上。 我们需要一个实用程序来帮助我们在客户端获取数据库的快照。 如果您有一个实用程序可以捕获客户端数据库的状态,那么您可以在测试机器中加载它。 我们的想法是重现您的客户所面临的情况。 这样您就可以解决任何未发现的问题。
支持的功能
- 将模式保存为文本
- 查看模式
- 查看整个数据库
所需工具
- 来自 Microsoft 的 ODBC.NET 数据提供程序
- MySQL 数据库
- MySQL ODBC 连接器
使用代码
首先,添加对 Microsoft ODBC 的引用。 然后使用 using microsoft.odbc
语句告诉它您要使用 MS ODBC。 简而言之,OdbcConnection
将用于打开连接,OdbcCommand
用于执行查询,OdbcDataReader
用于读取结果集。 下面显示的代码记录了每个步骤。 您会注意到,它运行 MySQL 特定的命令 SHOW TABLES
以获取表列表。 然后,它基于该特定表运行另一个查询,SHOW COLUMNS IN CURRENT_TABLE
。 这就是所有代码的作用。
代码
/*/////////////////////////////////////////////////////////////////////////
//
@@ Function:
@f ::PrepareSchema
//
@@ Description:
@d when this is called the widget is updated and everything
// about this database and tables are displayed
//
@@ Type:
@t public
//
@@ Arguments:
// none.
@@ Returns:
@r void
//
@@ Preconditions:
@c Provided that the GUI is running and DB Connection is made.
//
@@ Postconditions:
@o DB schema displayed
//
@@ References:
@e Query MySql with C#.
//
/////////////////////////////////////////////////////////////////////////*/
public void PrepareSchema()
{
// create the connection object by setting the DSN
OdbcConnection ocConnection = new OdbcConnection("DSN="+ strDSN);
// second connection is created so we could make
// queries while executing one
OdbcConnection ocConnection2 = new OdbcConnection("DSN="+ strDSN);
// this will open up both connections
ocConnection.Open();
ocConnection2.Open();
// declare the commands for each table and column
OdbcCommand ocTableCommand;
OdbcCommand ocColumnCommand;
// create a command object. this will execute SHOW TABLES
// query. In mysql, it shows all of the tables contained in
// the database in use.
ocTableCommand = new OdbcCommand("SHOW TABLES", ocConnection);
// declare reader objects for tables and columns
OdbcDataReader odrTableReader;
OdbcDataReader odrColumnReader;
// queries that return result set are executed by ExecuteReader()
// If you are to run queries like insert, update, delete then
// you would invoke them by using ExecuteNonQuery()
odrTableReader = ocTableCommand.ExecuteReader();
// place create db statement in rich text box
rchtxtSchema.Text += "CREATE DATABASE ";
rchtxtSchema.Text += ocConnection.Database;
rchtxtSchema.Text += ";\r\n\r\n";
rchtxtSchema.Text += "USE DATABASE ";
rchtxtSchema.Text += ocConnection.Database;
rchtxtSchema.Text += ";\r\n\r\n";
string strTable = "";
string strColumnName = "";
string strColumnType = "";
string strColumnNull = "";
string strColumnPKey = "";
string strColumnDflt = "";
string strColumnExtr = "";
// reader the set of tables
while(odrTableReader.Read())
{
// here we are expecting rows with only 1 column
// containing the table name. that's why explcity
// call GetString() at 0th index
strTable = odrTableReader.GetString(0);
rchtxtSchema.Text += "CREATE TABLE ";
rchtxtSchema.Text += strTable;
rchtxtSchema.Text += "\r\n(\r\n";
// build up the command for each table
ocColumnCommand = new OdbcCommand("SHOW COLUMNS IN " +
strTable, ocConnection2);
// run the query
odrColumnReader = ocColumnCommand.ExecuteReader();
// reading the set of columsn
while(odrColumnReader.Read())
{
// This query returns the name of column, Type,
// wherther it's Null, whether it's primary Key,
// the default value, and extra info such as
// whether it's autoincrement or not
strColumnName = odrColumnReader.GetString(0);
strColumnType = odrColumnReader.GetString(1);
strColumnNull = odrColumnReader.GetString(2);
strColumnPKey = odrColumnReader.GetString(3);
//strColumnDflt = odrColumnReader.GetString(4);
strColumnExtr = odrColumnReader.GetString(5);
if (!strColumnNull.Equals("YES"))
strColumnNull = " NOT NULL ";
else
strColumnNull = "";
if (strColumnPKey.Equals("PRI"))
strColumnPKey = " PRIMARY KEY ";
//this.rchtxtSchema.Text += "\n";
rchtxtSchema.Text += " ";
rchtxtSchema.Text += strColumnName;
rchtxtSchema.Text += " ";
rchtxtSchema.Text += strColumnType;
rchtxtSchema.Text += strColumnPKey;
rchtxtSchema.Text += strColumnNull;
rchtxtSchema.Text += ",";
rchtxtSchema.Text += "\r\n";
}
rchtxtSchema.Text = this.rchtxtSchema.Text.Substring(0,
this.rchtxtSchema.Text.Length-3);
rchtxtSchema.Text += "\r\n);\r\n\r\n";
// free up the reader object
odrColumnReader.Close();
}
// close the reader
odrTableReader.Close();
// disconnect
ocConnection.Close();
ocConnection2.Close();
}
关注点
最初,为了实现这一点,我一直在 ODBC、ADODB 和 OLEDB 之间来回切换。 根据 MySQL 的说法,使用 OLEDB 并不安全。 没有提到如何利用 OLEDB 来执行简单的数据库任务。 最后,决定使用 ODBC 来完成这项工作会非常简单。 您可能已经注意到,我使用了特定数据库提供程序使用的内置命令(即 show tables
)。 我绝对欢迎任何建议或与 MySQL 配合使用的标准的工作示例。
如何使用此演示
保存模式
首先,单击“选择目标数据库”。 这应该会产生一个对话框,显示系统和用户 DSN 的列表。
图 2.0 - 描绘了 DSN 对话框。
现在,单击“另存为模式文件”并选择要保存文件的位置。
图 3.0 - 描绘了文件保存对话框。
接下来,确保选中“保存模式”。 然后单击“运行”。
图 4.0 - 描绘了操作的状态。 在此示例中,程序成功写入 C:\s01user38_schema.txt。
查看模式
首先,取消选中“保存模式”,然后选中“查看模式”。 它应该产生如图 5.0 所示的输出
图 5.0 - 描绘了 s01user38 的模式。
查看数据库
现在,单击“在网格中查看数据库”,然后按“运行”。