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();
    }
}