asp.net执行无参数与有参数的存储过程
//执行存储过程返回值
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;
}
}