码迷,mamicode.com
首页 > 数据库 > 详细

.NET连接MySQL和SQL Server数据库

时间:2017-09-09 18:15:17      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:csdn   tab   点击   name   dataset   tostring   client   data   账号   

连接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数据库

标签:csdn   tab   点击   name   dataset   tostring   client   data   账号   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!