ashx读写MySql数据库

2021-01-21 17:33:15 浏览数 (1)

点击下载MySql.Data.dll

引用

代码语言:javascript复制
using MySql.Data.MySqlClient;
代码语言:javascript复制
MySqlConnection con = new MySqlConnection();

数据库连接配置

代码语言:javascript复制
/// <summary>
/// 数据库连接配置
/// </summary>
public void OpenDatabase()
{
    con.ConnectionString = "Data Source=连接地址;User ID=登录名;Password=密码;DataBase=数据库名称";
    con.Open();
}

查询表数据

代码语言:javascript复制
/// <summary>
/// 查询contents表
/// </summary>
/// <returns></returns>
public string GetContents() {
    string Body = "";
    OpenDatabase();
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM contents", con);
    MySqlDataReader dr = cmd.ExecuteReader();
    //循环数据
    while (dr.Read())
    {
        Body  = dr["title"].ToString() "n";
    }
    dr.Close();
    con.Close();
    return Body;
}

0 人点赞