首页 > 代码库 > C# BulletedList 绑定 Mysql

C# BulletedList 绑定 Mysql

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 MySql.Data.MySqlClient;

public partial class page1 : System.Web.UI.Page
{
    string connStr = String.Format(
        "server={0};user id={1};password={2};database={3};port={4};pooling=false;charset=utf8",
        "192.168.1.3", "root", "1234567", "test", 3306);

    public MySqlConnection con = new MySqlConnection();
    public MySqlDataAdapter da;
    public MySqlCommandBuilder cb;
    public MySqlCommand cmd;
    public MySqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = connStr;
       
        try
        {
            con.Open();
            Label1.Text = "连接 Mysql 成功!";

            string sql = "select * from test";
            da = new MySqlDataAdapter(sql, con);
            DataSet mydataset = new DataSet();
            da.Fill(mydataset, "test");
            BulletedList1.DataSource = mydataset.Tables["test"];
            BulletedList1.DataTextField = "Name";
            BulletedList1.DataValueField = "link";
            BulletedList1.DataBind();
        }
        catch
        {
            Label1.Text = "连接 Mysql 失败!";
        }
    }
}