.NET连接MySQL和SQL Server数据库

连接MySQL:

首先,我们需要下载一个程序包,并解压“MySql.Data.dll”,点击下载

using MySql.Data.MySqlClient;
MySqlConnection mycon = new MySqlConnection("server=xxx.xxx.xx.xxx;user id=sa;password=sa;database=Wan");
mycon.Open();
MySqlCommand mysqlcom = new MySqlCommand("select Password_ from Person where Username='" + Username + "'", mycon);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = mysqlcom;
DataSet ds = new DataSet();
sda.Fill(ds);
string Password_ = null;
try
{
    Password_ = ds.Tables[0].Rows[0]["Password_"].ToString();
}
catch
{
    return "该账号未注册";
}
try
{
    if (Password_.Equals(Password))
    {
        return "账号和密码正确";
    }
    else
    {
        return "密码错误";
    }
}
finally
{
    mysqlcom.Dispose();
    mycon.Dispose();
    mycon.Close();
}

连接SQL Server:

SqlConnection mycon = new SqlConnection("server=.;database=Wan;uid=sa;pwd=sa");
con.Open();
SqlCommand sqlcom = new SqlCommand("select Password_ from Person where Username='" + Username + "'", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = sqlcom;
DataSet ds = new DataSet();
sda.Fill(ds);
string Password_ = null;
try
{
    Password_ = ds.Tables[0].Rows[0]["Password_"].ToString();
}
catch
{
    return "该账号未注册";
}
try
{
    if (Password_.Equals(Password))
    {
        return "账号和密码正确";
    }
    else
    {
        return "密码错误";
    }
}
finally
{
    sqlcom.Dispose();
    con.Dispose();
    con.Close();
}    


原文地址:https://www.cnblogs.com/wyongqi/p/7498761.html