ado| Basic Tutorials
The previous section talked about how to use ado.net to query data, which describes how to run SQL Select, INSERT, and UPDATE commands and stored procedures
1. Stored Procedure
C # 's code is as follows:
<summary>
Stored Procedure parameter structure
</summary>
public struct Procparam
{
public string paramname;
public string paramvalue;
Public System.Data.SqlDbType Paramtype;
}
<summary>
Executes a stored procedure that returns a dataset
</summary>
<param name= "strprocname" > Stored procedure name </param>
<param name= "Alparamname" > Parameter name </param>
<param name= "Alparamvalue" > Parameter value </param>
<param name= "constring" > Data connection string </param>
<returns></returns>
public static System.Data.SqlClient.SqlDataReader execprocedure (String strprocname,system.collections.arraylist Alparamname,system.collections.arraylist alparamvalue,string constring)
{
System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
cn. Open ();
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
Cmd. Connection =CN;
Cmd.commandtype =system.data.commandtype.storedprocedure;
Cmd.commandtext =strprocname;
for (int i=0;i<alparamname.count; i++)
{
System.Data.SqlClient.SqlParameter param=new System.Data.SqlClient.SqlParameter (alparamname[i). ToString (), Alparamvalue[i]. ToString ());
Cmd. Parameters.Add (param);
}
System.Data.SqlClient.SqlDataReader Dr;
Dr=cmd. ExecuteReader ();
Return Dr;
}
The above code can execute a stored procedure of any parameter (returns a dataset)
2. Direct SQL command
<summary>
Executes a SQL SCRIPT that returns a string
</summary>
<param name= "constring" > Connection string </param>
<param name= "sql_command" >sql command </param>
<returns> Return Data Set </returns>
public static string Getdatastring (String sql_command,string constring)
{
System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
cn. Open ();
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
Cmd. Connection =CN;
Cmd.commandtype =system.data.commandtype.text;
Cmd.commandtext =sql_command;
String Str= "";
Try
{
Str=cmd. ExecuteScalar (). ToString ();
}
catch (System.Data.SqlClient.SqlException e)
{
System.Diagnostics.Debug.WriteLine (E.message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine (ex. message);
}
return str;
}
<summary>
Executes a SQL that is not returned
</summary>
<param name= "Sql_command" ></param>
<param name= "Constring" ></param>
<returns></returns>
public static void ExecuteSQL (String sql_command,string constring)
{
System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
cn. Open ();
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
Cmd. Connection =CN;
Cmd.commandtype =system.data.commandtype.text;
Cmd.commandtext =sql_command;
Try
{
Cmd. ExecuteNonQuery ();
}
catch (System.Data.SqlClient.SqlException e)
{
System.Diagnostics.Debug.WriteLine (E.message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine (ex. message);
}
}
The above code suggestion puts in a class, if has the question please contact me msn:ilxc8292@hotmail.com