跳至內容

ASP.NET/Database

維基教科書,自由的教學讀本

ASP.NET示例

[編輯]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string constr = "server=localhost;user=root;database=world;port=3306;password=";
        string constr = ConfigurationManager.ConnectionStrings["localMySQL"].ConnectionString.ToString();
        MySqlConnection con = new MySqlConnection(constr);
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select * from city", con);
        MySqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        con.Close();
    }
}