当前位置: 首页 > 工具软件 > SharpDevelop > 使用案例 >

sharpdevelop mysql_使用sharpdevelop连接MySQL

陶涵育
2023-12-01

MySQL版本为:5.0.88;

sharpdevelop版本3.1

综合网上查找的信息,通过MySQL.DATA这个类来连接数据库,其特性和ODBC一样,其中的方法和属性也基本一致。

首先去MySQL网页上下载5.0.9版本的Connector。

安装好以后可以在/windows/assembly文件夹找到MySQL.DATA这个组件。这时候已经将这个组件注册到全局程序集中了,

我们就可以在sharpdevelop中使用该组件。

打开sharpdevelop和要使用MySQL数据库的工程,在展开的工程中右键点击引用,选择添加引用。在弹出的GAC对话框中选中MySQL.Data并点击右边的选择,将该引用加入到该工程。在文件的顶部加入 using Mysql.Data.MysqlClient;后即可使用MySqlConnection、MySqlCommand、MySqlDataReader、MysqlDataAdapter、MysqlGrid等方法进行数据库操作。

以下是官方给出的示例代码。

/*

* Created by SharpDevelop.

* User: Lecenio Helio Trein Junior - lecenoi@yahoo.com.br

* Date: 3/23/2005

* Time: 13:56

*

*/

using System;

using System.Drawing;

using System.Windows.Forms;

using System.Data;

using MySql.Data.MySqlClient;

namespace MySqlSample

{

///

/// Application to show how to connect with mySQL

///

public class MainForm : System.Windows.Forms.Form

{

private System.Windows.Forms.DataGrid MyDataGrid;

private MySqlConnection MyConn;

private MySqlDataAdapter MyAdapter;

private DataSet MyDataSet;

public MainForm()

{

InitializeComponent();

MyDataSet = new DataSet();

MyConn = new MySqlConnection("Database=myDatabase;Data Source=10.0.4.217;User Id=lecenio;Password=senha");

try{

MyConn.Open();

}

catch(System.Exception e)

{

MessageBox.Show(e.Message.ToString());

}

if (MyConn.State == ConnectionState.Open)

{

MyAdapter = new MySqlDataAdapter("SELECT * FROM myTable", MyConn);

MyAdapter.Fill(MyDataSet, "MyTable");

MyDataGrid.DataSource = MyDataSet;

MyDataGrid.DataMember = "MyTable";

}

}

protected override void Dispose(bool disposing)

{

if (disposing)

{

if (MyConn.State == ConnectionState.Open)

{

MyConn.Close();

}

}

base.Dispose(disposing);

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new MainForm());

}

#region Windows Forms Designer generated code

///

/// This method is required for Windows Forms designer support.

/// Do not change the method contents inside the source code editor. The Forms designer might

/// not be able to load this method if it was changed manually.

///

private void InitializeComponent() {

this.MyDataGrid = new System.Windows.Forms.DataGrid();

((System.ComponentModel.ISupportInitialize)(this.MyDataGrid)).BeginInit();

this.SuspendLayout();

//

// MyDataGrid

//

this.MyDataGrid.DataMember = "";

this.MyDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;

this.MyDataGrid.Location = new System.Drawing.Point(16, 8);

this.MyDataGrid.Name = "MyDataGrid";

this.MyDataGrid.Size = new System.Drawing.Size(352, 336);

this.MyDataGrid.TabIndex = 0;

//

// MainForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(384, 357);

this.Controls.Add(this.MyDataGrid);

this.Name = "MainForm";

this.Text = "MainForm";

((System.ComponentModel.ISupportInitialize)(this.MyDataGrid)).EndInit();

this.ResumeLayout(false);

}

#endregion

}

}

sharpdevelop网站的wiki给出的解决方案点这里。

 类似资料: