ADO.NET 基础教程(二)


所属类别:.NET

文章作者:未知

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


上一节讲到了怎样使用ADO.NET来查询数据,这节讲怎么样运行SQL的SELECT、INSERT和UPDATE命令及存储过程1、存储过程C#的代码如下:/// /// 存储过程参数结构/// public struct ProcParam{public string ParamName;public string ParamValue;public System.Data.SqlDbType ParamType ;}/// /// 执行一个返回数据集的存储过程/// /// 存储过程名称/// 参数名称/// 参数值/// 数据连接串/// 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/// 执行一个返回字符串的SQL SCRIPT/// /// 连接串/// SQL命令/// 返回数据集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;}/// /// 执行一个无返回的SQL/// /// /// /// 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 );}}以上代码建议放在一个类中,如果有问题请与我联系msn:ilxc8292@hotmail.com关闭本页

相关信息

· DB2 如何提高IMPORT命令性能

· 增强Linux/Unix服务器系统安全性介绍

· 向Web服务器上传文件

· RFC2045译文(7)








....

96464 81871