2016-06-12 98 views
1

我不知道這些兩件事情有什麼用連接和命令對象時,插入,更新和刪除被稱爲從SQL Server存儲過程的區別。連接,命令對象,在SQL Server

例如有關連接:

Cn.Execute "Exec ProcedureName" 

有關命令的obj:

Cmd.CommandType=AdCmdStoredProc 
CmdCommandText="....." 
Cmd.ActiveConnection=Cn 
Cmd.Parameters.Append ..... 
........ 

我真的不知道,因爲它們看起來相似時使用它們。

在此先感謝

回答

4

有這麼多的文章,這說明在Ado.Net使用命令和Connection對象。少數是如下:

http://www.c-sharpcorner.com/UploadFile/c5c6e2/working-with-command-object/

http://www.c-sharpcorner.com/uploadfile/mahesh/connection-object-in-ado-net/

http://csharp.net-informations.com/data-providers/csharp-ado.net-connection.htm

連接對象: 連接對象是用於創建應用程序之間的鏈接數據的基本Ado.Net部件資源。所以你定義了一個連接字符串,你可以使用它來初始化到你的數據源的連接。

Command對象: Command對象是用於執行使用連接對象對你的數據源查詢另一個Ado.Net組件。所以基本上你需要連接和命令對象來在你的數據源上執行查詢。使用命令對象,你可以執行嵌入式查詢,存儲過程等

示例代碼:

 SqlConnection con = new SqlConnection(connectionString); // creates object of connection and pass the connection string to the constructor 
     SqlCommand cmd = new SqlCommand(); // creates object of command 
     cmd.Connection = con; // tells command object about connection, where the query should be fired 
     cmd.CommandText = "Query"; // your query will go here 
     cmd.CommandType = System.Data.CommandType.Text; // it tells the command type which can be text, stored procedure 

     con.Open(); // opens the connection to datasource 
     var result = cmd.ExecuteReader(); // executes the query on datasource using command object 
     con.Close(); // closes the connection 

我希望這會幫助你。 :)

+0

OP中的代碼看起來像經典ADO而不是ADO.NET –

+0

是的,基本上ADO和ADO.Net中的代碼是相同的,不同之處在於性能以及它們在後臺工作的方式。 –