要用到using System.Data.SqlClient;//名稱空間
SQL 查詢
string connection_str=@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ChatRoom;Data Source=.\sqlexpress";
string select_str="select 欄位 from 表名 [where 欄位=值]";//"[之間的內容]"是可選的
SqlConnection con=new SqlConnection(connection_str);//一,建立資料庫連線物件
SqlCommand com=new SqlCommand(select_str,con);//二,建立資料操作物件
con.Open();//現在用的是連線操作方法,所以要先開啟這個資料連線物件的連線
DataReader _dataReader = com.ExcuteReader();
while(_dataReader.Next())//遍歷
{
string temp += dataReader["欄位"].ToString()+"\r\n";
}
con.Close();//關閉資料庫連線物件
MessageBox.Show(temp);
以上這種方法是稱為連線式操作。
以下這種方法是稱為非連線式操作。
using System.Data.SqlClient;//因為要用到SQL物件
using System.Data;//要用到資料集物件,如以下將要用到:DataSet物件
SqlDataAdapter DataAdapte
要用到using System.Data.SqlClient;//名稱空間
SQL 查詢
string connection_str=@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ChatRoom;Data Source=.\sqlexpress";
string select_str="select 欄位 from 表名 [where 欄位=值]";//"[之間的內容]"是可選的
SqlConnection con=new SqlConnection(connection_str);//一,建立資料庫連線物件
SqlCommand com=new SqlCommand(select_str,con);//二,建立資料操作物件
con.Open();//現在用的是連線操作方法,所以要先開啟這個資料連線物件的連線
DataReader _dataReader = com.ExcuteReader();
while(_dataReader.Next())//遍歷
{
string temp += dataReader["欄位"].ToString()+"\r\n";
}
con.Close();//關閉資料庫連線物件
MessageBox.Show(temp);
以上這種方法是稱為連線式操作。
以下這種方法是稱為非連線式操作。
using System.Data.SqlClient;//因為要用到SQL物件
using System.Data;//要用到資料集物件,如以下將要用到:DataSet物件
string connection_str=@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ChatRoom;Data Source=.\sqlexpress";
string select_str="select 欄位 from 表名 [where 欄位=值]";//"[之間的內容]"是可選的
SqlConnection con=new SqlConnection(connection_str);//一,建立資料庫連線物件
SqlDataAdapter DataAdapte