Ado.net Basic Course (ii)

Source: Internet
Author: User
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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.