asp.net执行无参数与有参数的存储过程
2009-06-27 15:45
316 查看
/// <summary>
/// 得到数据表department满足查询条件的记录数
/// </summary>
/// <param name="where">查询条件</param>
/// <param name="recordCount">记录数</param>
public void Select(string where, out int recordCount)
{
string sqlCommand = "departmentCountByWhere";
SqlParameter[] param ={
new SqlParameter("@where",SqlDbType.VarChar,8000),
new SqlParameter("@recordCount",SqlDbType.Int)
};
param[0].Value = where;
param[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(Conn.SqlConn, CommandType.StoredProcedure, sqlCommand, param);
recordCount = Convert.ToInt32(param[1].Value);
}
执行存储过程返回值
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;
}
} 文章来自学IT网:http://www.xueit.com/html/2008-10/117_3948_00.html
/// 得到数据表department满足查询条件的记录数
/// </summary>
/// <param name="where">查询条件</param>
/// <param name="recordCount">记录数</param>
public void Select(string where, out int recordCount)
{
string sqlCommand = "departmentCountByWhere";
SqlParameter[] param ={
new SqlParameter("@where",SqlDbType.VarChar,8000),
new SqlParameter("@recordCount",SqlDbType.Int)
};
param[0].Value = where;
param[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(Conn.SqlConn, CommandType.StoredProcedure, sqlCommand, param);
recordCount = Convert.ToInt32(param[1].Value);
}
执行存储过程返回值
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;
}
} 文章来自学IT网:http://www.xueit.com/html/2008-10/117_3948_00.html
相关文章推荐
- ASP.Net执行存储过程,SqlDbType.Decimal作为输出参数,没有小数位值的解决办法
- asp.net 执行存储过程 无输出参数 有两个输入参数
- Asp.net中利用ExecuteNonQuery()执行存储过程返回-1解决方案
- asp.net 调用 存储过程(带output 参数)
- ASP.NET中调用存储过程带(输入/输出)参数和返回值
- ASP.NET执行存储过程,并获取存储过程的返回值
- asp.net 获得存储过程 返回值,影响行数,输出参数 output 用法
- asp.net 操作带输入输出参数的分页存储过程(三)
- asp.net/C# 执行存储过程技术要点,获取Return返回值,解决“存储过程总返回-1”的问题
- Asp.Net中调用存储过程并返回输出参数
- ASP.NET调用存储过程返回输出参数,获得返回值
- asp.net执行存储过程
- ASP.NET中调用存储过程带(输入/输出)参数和返回值的一些常用的例子
- ASP.NET中调用存储过程带(输入/输出)参数和返回值
- ASP.NET执行存储过程
- Asp.net中利用ExecuteNonQuery()执行存储过程返回-1解决方案
- ASP.net(c#) 在ACCESS数据库中利用参数使用存储过程例子(防SQL注入)
- ASP.NET中调用存储过程带(输入/输出)参数和返回值的一些常用的例子
- ASP.Net 调用存储过程,同时返回记录集和输出参数
- ASP.NET执行存储过程,并获取存储过程的返回值