龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

asp.net执行无参数与有参数的存储过程

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
//执行存储过程返回值 SqlParameter[] para = new SqlParameter[2]; //建立参数 para[0] = new SqlParameter("@type", "深圳"); para[1] = new SqlParameter("@result", SqlDbType.Int, 1); para[1].Direction = ParameterDirection.Output; /

//执行存储过程返回值
SqlParameter[] para = new SqlParameter[2];    //建立参数
para[0] = new SqlParameter("@type", "深圳");
para[1] = new SqlParameter("@result", SqlDbType.Int, 1);
para[1].Direction = ParameterDirection.Output;  //输出返回值
SqlHelper.ExecuteNonQuery(SqlHelper.dbconn(), CommandType.StoredProcedure, "testPro", para);
Response.Write(para[1].Value.ToString());

返回DataTable(执行SQL语句)
DataTable dt = SqlHelper.ExecuteProDataTable(SqlHelper.dbconn(), CommandType.Text, "select top 10 * from dyj_client", null);

返回DataTable(执行存储过程方式)
SqlParameter[] para = new SqlParameter[1];
// para[0] = new SqlParameter("@type", "深圳");
para[0] = new SqlParameter("@type",SqlDbType.VarChar, 20);
para[0].Value = "深圳";
DataTable dt = SqlHelper.ExecuteProDataTable(SqlHelper.dbconn(), CommandType.StoredProcedure, "testPro", para);
foreach (DataRow dr in dt.Rows)
{
    Response.Write(dr[0].ToString() + "<br>");
}

SqlHelper中的函数
public static DataTable ExecuteProDataTable(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)//(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
 if (connection == null) throw new ArgumentNullException("connection");

 // 预处理
 SqlCommand cmd = new SqlCommand();
 isconnection(connection);
 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
 // 创建DbDataAdapter与ataTable
 using (SqlDataAdapter da = new SqlDataAdapter(cmd))
 {
     DataTable dt = new DataTable();
     da.Fill(dt);
     connection.Close();

     return dt;
 }
}

收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!

热评话题

按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接