1. 程式人生 > >使用ADO.NET訪問資料庫 使用ADO.NET訪問資料庫

使用ADO.NET訪問資料庫 使用ADO.NET訪問資料庫

使用ADO.NET訪問資料庫

 

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class Program
{
static void Main(string[] args)
{
#region 連線資料庫
//步驟一:配置引數(連線到伺服器,連線的資料庫名稱,使用者名稱,密碼)
string str = "Data Source=.;Initial Catalog=Myschool;User ID=sa;pwd=1";
//步驟二:建立Connection物件連線資料庫(SqlConnection)
SqlConnection con = new SqlConnection(str);
//步驟三:開啟資料庫
con.Open();
Console.WriteLine("開啟資料庫成功!");

//步驟N:將資料庫關閉
con.Close();
Console.WriteLine("關閉資料庫成功!");
#endregion

#region 資料庫異常
try
{
con.Open();

}
catch(SqlException ex){
Console.WriteLine("出現異常"+ex); 
}
catch (Exception ex)
{
Console.WriteLine("出現異常!" + ex);
}
finally {
con.Close();
Console.WriteLine("關閉資料庫成功!");
}
#endregion

#region 登入
Console.WriteLine("請輸入使用者名稱:");
string loginID = Console.ReadLine();
Console.WriteLine("請輸入密碼:");
string loginPwd = Console.ReadLine();
User user = new User();
user.login(loginID, loginPwd);
#endregion
Console.ReadLine();
}
}
}

 

ConnectionDB類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class ConnectionDB
{
static string str = "Data Source=.;Initial Catalog=Myschool;User ID=sa;password=1";
public SqlConnection con = new SqlConnection(str);
public void OpenDB() 
{
try
{
con.Open();
}
catch (Exception ex)
{

Console.WriteLine("發生異常!"+ex);
}
}
public void CloseDB() 
{
con.Close();
}
}
}

 

User類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class User
{
ConnectionDB bd = new ConnectionDB();
public void login(string StudentNo, string loingPwd)
{
string sql = "SELECT COUNT(1) FROM Student WHERE StudentNo='" + StudentNo + "' AND Loginpwd='" + loingPwd + "'";
Console.WriteLine(sql);
bd.OpenDB();
SqlCommand cmd = new SqlCommand(sql, bd.con);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
Console.WriteLine("登入成功!");
}
else
{
Console.WriteLine("登入失敗!");
}

}


}
}

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class Program
{
static void Main(string[] args)
{
#region 連線資料庫
//步驟一:配置引數(連線到伺服器,連線的資料庫名稱,使用者名稱,密碼)
string str = "Data Source=.;Initial Catalog=Myschool;User ID=sa;pwd=1";
//步驟二:建立Connection物件連線資料庫(SqlConnection)
SqlConnection con = new SqlConnection(str);
//步驟三:開啟資料庫
con.Open();
Console.WriteLine("開啟資料庫成功!");

//步驟N:將資料庫關閉
con.Close();
Console.WriteLine("關閉資料庫成功!");
#endregion

#region 資料庫異常
try
{
con.Open();

}
catch(SqlException ex){
Console.WriteLine("出現異常"+ex); 
}
catch (Exception ex)
{
Console.WriteLine("出現異常!" + ex);
}
finally {
con.Close();
Console.WriteLine("關閉資料庫成功!");
}
#endregion

#region 登入
Console.WriteLine("請輸入使用者名稱:");
string loginID = Console.ReadLine();
Console.WriteLine("請輸入密碼:");
string loginPwd = Console.ReadLine();
User user = new User();
user.login(loginID, loginPwd);
#endregion
Console.ReadLine();
}
}
}

 

ConnectionDB類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class ConnectionDB
{
static string str = "Data Source=.;Initial Catalog=Myschool;User ID=sa;password=1";
public SqlConnection con = new SqlConnection(str);
public void OpenDB() 
{
try
{
con.Open();
}
catch (Exception ex)
{

Console.WriteLine("發生異常!"+ex);
}
}
public void CloseDB() 
{
con.Close();
}
}
}

 

User類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 開啟資料庫
{
class User
{
ConnectionDB bd = new ConnectionDB();
public void login(string StudentNo, string loingPwd)
{
string sql = "SELECT COUNT(1) FROM Student WHERE StudentNo='" + StudentNo + "' AND Loginpwd='" + loingPwd + "'";
Console.WriteLine(sql);
bd.OpenDB();
SqlCommand cmd = new SqlCommand(sql, bd.con);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
Console.WriteLine("登入成功!");
}
else
{
Console.WriteLine("登入失敗!");
}

}


}
}