?? ado.txt
字號:
some CODES : ----080325AM
(我寫這些代碼的目的不是純為了實驗時候需要,而是相應的學習,剛剛在VS2005中寫了一段,發現一些問題,
我學的ADO.NET 1.1版本的知識類現在都被VS2005版封裝得更隱蔽了,但是這些代碼還是很有用的,只有理解了這些,才可以在更高版本的IDE中掌控自如.現在都有VS2008出來了,但我還是要學2005的,因為英文水平不夠,
一方面也得給自己鼓氣,認真學英文!而對于這些代碼,我是專門從一門好書上摘錄下來的,應該是我所接受的...)
1.1
private void SQLConectionComponent()
{ //使用VS對象sqlConnection進行連接
sqlConnection1.connectionString="SERVER;"+"INTERGATED SECURITY=Ture"; //下劃線表示連接
使用的是賬號身份驗證
try
{ //Try-Catch循環使您應用程序能捕獲任何異常,并用預先定義好的方式處理
sqlConnection1.Open();
}
catch(Exception ex) //ex為Exception對象一個簡單實例
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
sqlConnection1.Close();
} //可視化連接組件必須添加導入命名空間的指令,--"Using System.Data.SqlClient;"
這樣就可以簡化一些對象命令
1.2
private void SQLConnectionString(string sServer,string sUser,string sPwd)
{
SqlConnection cn = new sqlConnection();
cn.ConnectionString="SERVER="+sServer+";"+UID="+sUser+";PWD+sPwd;
try
{
cn.Open();
}
catch(Exception ex)
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
cn.Close();
private void SQLConnectionSSPI(string sServer)
{//打開信任連接
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture");
try
{
cn.Open();
}
catch(Exception ex)
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
cn.Close();
}
1.3 連接池使用,可以打開多個連接,在上面代碼中多新建一個對象實例cn2即可;
2.1
private void SQLConectionComponent(string sServer, string sDB)
{
sqlConnection1.connectionString="SERVER;"+"INTERGATED SECURITY=Ture";DATABASE="+sDB;
sqlCommand1.CommandText="select * from customers";
//set the active connection
sqlCommand1.connection=sqlConnection1;
try
{
sqlConnection1.Open();
System.Data.SqlClient.SqlDataReader dr = sqlCommand1.ExecuteReader();
//若添加命名空間則此代碼可以簡寫!;
這里創建SqlDataReader對象dr,SqlDataReader命令會從目標數據源中返回一個快速的只向前的數據流.
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlConnection1.Close();
}
2.2
private void SQLConectionComponent(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
string sSQL;
SqlCommand cmd = new SqlCommand(" ",cn); //這里帶有兩個參數.第一個為字符串,初始為空;第二個為 SqlConnection對象.它與SQLConectionComponent(string sServer, string sDB)中的兩個參數要匹配!
try
{
cn.Open();
sSQL="IF EXISTS"+"(SELECT * FROM dbo.Sysobjects"+"where id = object_id(N'[Department]')"
+"AND objectproperty (id,N'IsUserTable')=1"+"DROP Table [Department]";
cmd.ExcuteNoQuery();
// After drop the table;then create the table
sSQL="CREATE Table Cepartment"+"(DepartmentID Int NOT NULL,"+"DepartmentName char(25),"
+"Primary key (DepartmentID))";
cmd.CommandText=sSQL;
cmd.ExecuteNoQuery(); //此方法用于在聯機的數據源上執行SQL語句,它用于DDL語句和動作查詢,
如Insert,Update,Delete等.該方法返回受影響的行數,而不返回輸出參數或結果集.
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close(); // 關閉連接
}
2.3 執行參數化的SQL語句
private void SQLConectionComponent(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
//set up the command object's parpmeter types
SqlCommand cmd = new SqlCommand("INSERT INTO Department VALUES"+"(@DepartmentID,@DepartmentName)",cn);
//以上這句的參數也要和SQLConectionComponent(string sServer, string sDB)匹配!
SqlParameter parmDepartmentID = new SqlParameter("@DepartmentID",SqlDbType.Int);
parmDepartmentID.Direction=ParameterDirection.Input;//參數為輸出類型.Input為SqlParameterDirection的枚舉值
SqlParameter parmDepartmentName = new SqlParameter("@DepartmentName",SqlDbType.Char,25);
parmDepartmentName.Direction=ParameterDirection.Input;
//Add the parameter object to the command parameter's collection.
cmd.Parameters.Add(parmDepartmentID);
cmd.Parameters.Add(parmDepartmentName);
try
{//open...& prepare the command.
cn.Open();
cmd.Preapare();
//execute the prepared SQL statement to insert 10 rows
for(int i=1;i<=10;i++)
{
parmDepartmentID.Value = i;
parmDepartmentName.Value = "New Department" + i;
cmd.ExecuteNoQuery();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close();
}
說明:1)注意在SQL中使用的參數標記的格式;
參數標記用于SQL中可替換的字符,運行時,這些參數會被Sqlcommand對象的Parameter機和所提供的值替換.
2)ADO或OleDbCommand 對象使用問號(?)來表示可替換的字符,而Sqlcommand對象要求所有的參數標記都已@字符開頭.
2.4 執行帶有返回值的存儲過程
/這里補充一點,我對數據庫中的存儲過程的概念還使模糊的,什么時間還得研究研究...
/這是我臨時找到的一篇不錯的文章,是解說存儲過程和觸發器的...http://www.yscode.com/article/database/303.html
先在Query Analyzer中執行以下代碼(用于創建存儲過程,創建添加到Northwind中的StockValue存儲過程)
CREATE PROCEDURE StockVaule
@ProductID int
AS
DECLARE @StockVaule money
SELECT StockVaule =(Units InStock * UnitPrice)
FROM Products where ProductID = @ProductID
RETURN @StockVaule
private void SQLCommandPSScalar(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
//create the command object and set the SQL statment
SqlCommand cmd = new SqlCommand("StockVaule",cn);
cmd.CommandeType=CommandeType.StoredProedure; //屬性StoredProedure指定Command對象的參數為存儲過程
//create the parameter
cmd.Parameters.Add("@ProductID",SqlDbType.Int);
cmd.Parameters["@ProductID"].Direction=ParameterDirection.Input;
cmd.Parameters["@ProductID"].Vaule=1; //值1將被傳遞給存儲過程
try
{
decimal nStockVaule;
cn.Open();
nStockVaule=(decimal)cmd.ExecuteScalar(); //執行存儲過程
txtMidText=nStockVaule.Tostring();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close(); // 關閉連接
}
說明:StockVaule為存儲過程名稱;
CommandeType屬性有三個,StoredProedure--該命令為存儲過程;TableDirect---為數據庫表名稱;Text---為SQL語句.
ExecuteScalar 方法用于執行返回單個標量值的存儲過程或SQL語句,并把結果集的第一行上第一列返回給調用程序,而忽略返回值.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -