首页 > 数据库技术 > 详细

.NET连接MySQL和SQL Server数据库

时间:2017-09-09 17:53:25      阅读:256      评论:0      收藏:0      [点我收藏+]

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


.NET连接MySQL和SQL Server数据库

原文:http://www.cnblogs.com/wyongqi/p/7498761.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!