最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net 连接mysql数据库代码
时间:2022-06-25 06:04:36 编辑:袖梨 来源:一聚教程网
new OdbcConnection连接核心代码
private void MainClass_Load(object sender, System.EventArgs e)
{
string connectionString = @"Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;";OdbcConnection conn= new OdbcConnection(connectionString);
conn.Open();OdbcDataAdapter da = new OdbcDataAdapter ("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn);
DataSet ds = new DataSet("Cust");
da.Fill(ds, "Customers");dataGrid1.DataSource = ds.DefaultViewManager;
conn.Close();
}
下面为详细实例
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Data.Odbc;public class MainClass : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;public MainClass()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(376, 288);
this.dataGrid1.TabIndex = 0;
//
// MainClass
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 302);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Load += new System.EventHandler(this.MainClass_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);}
[STAThread]
static void Main()
{
Application.Run(new MainClass());
}private void MainClass_Load(object sender, System.EventArgs e)
{
string connectionString = @"Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;";OdbcConnection conn= new OdbcConnection(connectionString);
conn.Open();OdbcDataAdapter da = new OdbcDataAdapter ("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn);
DataSet ds = new DataSet("Cust");
da.Fill(ds, "Customers");dataGrid1.DataSource = ds.DefaultViewManager;
conn.Close();
}
}
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21