2016-06-12 5 views
1

SQL Serverからストアドプロシージャと呼ばれる挿入、更新、および削除を行うときに、接続とコマンドオブジェクトを使用する方法の違いはわかりません。接続について例えばSQL Serverの接続vsコマンドオブジェクト

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

接続オブジェクト: Connectionオブジェクトデータに、アプリケーション間のリンクを作成するために使用される基本的なAdo.Netコンポーネントですソース。したがって、データソースへの接続を初期化するために使用できる接続文字列を定義します。

コマンドオブジェクト: コマンドオブジェクトは、接続オブジェクトを使用してデータソースに対してクエリを実行するために使用される別の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.NETではなく古典的なADOのように見えます。 –

+0

はい、基本的にADOとADO.Netのコードは同じですが、違いはパフォーマンスとバックグラウンドでの動作の違いです。 –