標籤: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資料庫