[转]ASP.Net中怎样获得存储过程传出的参数
转自:浪剑
//-------------------------------------------------------------------------------
//测试获得存储过程传出的参数
//OLEDB方式,测试通过
//-------------------------------------------------------------------------------
OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=cn;
if(cn.State== ConnectionState.Closed){
cn.Open();
}
myCommand.CommandText="ShowCmdTest";
myCommand.CommandType=System.Data.CommandType.StoredProcedure;
myCommand.CommandTimeout = 20;
OleDbParameter inPara=new OleDbParameter("@inA",OleDbType.VarChar,80);
inPara.Value="5";
inPara.Direction=ParameterDirection.Input;
myCommand.Parameters.Add(inPara);
OleDbParameter outPara=new OleDbParameter("@outB",OleDbType.VarChar,80);
outPara.Direction=ParameterDirection.Output;
myCommand.Parameters.Add(outPara);
myCommand.ExecuteNonQuery();
Response.Write(outPara.Value);
OleDbDataReader myReader = myCommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("SubjectName");
dt.Columns.Add("CreateDate");
while(myReader.Read())
{
DataRow workRow = dt.NewRow();
workRow["SubjectName"] = myReader.GetValue(1);
workRow["CreateDate"] = myReader.GetValue(2);
dt.Rows.Add(workRow);
//Response.Write(myReader.GetValue(0).ToString()+"<br>");
}
myReader.Close();
//BindGrid();
MyDataGrid.DataSource = dt.DefaultView;// myReader;
MyDataGrid.DataBind();
//-------------------------------------------------------------------------------
//测试获得存储过程传出的参数
//SqlClient方式,测试通过
//-------------------------------------------------------------------------------
//连接数据库
SqlConnection sqlconn=new SqlConnection();
sqlconn.ConnectionString="server=192.168.3.80;uid=sa;Password=rain;database=MyStudy";
//server=192.168.3.80;uid=sa;Password=rain;database=MyStudy";
sqlconn.Open();
SqlCommand com=new SqlCommand();
com.Connection=sqlconn;
com.CommandText="ShowCmdTest";
com.CommandType=CommandType.StoredProcedure;
SqlParameter[] p_htno=new SqlParameter[2];
p_htno[0] = new SqlParameter("@inA",SqlDbType.VarChar,80);
p_htno[1] = new SqlParameter("@outB",SqlDbType.VarChar,80);
p_htno[0].Value ="测试输入";
p_htno[1].Direction=ParameterDirection.Output;
//p_htno[1].Value = this.drop_character.Items[this.drop_character.SelectedIndex].Value;
for ( int i =0 ;i< p_htno.Length;i++)
{
com.Parameters.Add(p_htno[i]);
}
com.ExecuteNonQuery();
Response.Write(p_htno[1].Value.ToString()+"<br>测试输出成功!");
//测试获得存储过程传出的参数
//OLEDB方式,测试通过
//-------------------------------------------------------------------------------
OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=cn;
if(cn.State== ConnectionState.Closed){
cn.Open();
}
myCommand.CommandText="ShowCmdTest";
myCommand.CommandType=System.Data.CommandType.StoredProcedure;
myCommand.CommandTimeout = 20;
OleDbParameter inPara=new OleDbParameter("@inA",OleDbType.VarChar,80);
inPara.Value="5";
inPara.Direction=ParameterDirection.Input;
myCommand.Parameters.Add(inPara);
OleDbParameter outPara=new OleDbParameter("@outB",OleDbType.VarChar,80);
outPara.Direction=ParameterDirection.Output;
myCommand.Parameters.Add(outPara);
myCommand.ExecuteNonQuery();
Response.Write(outPara.Value);
OleDbDataReader myReader = myCommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("SubjectName");
dt.Columns.Add("CreateDate");
while(myReader.Read())
{
DataRow workRow = dt.NewRow();
workRow["SubjectName"] = myReader.GetValue(1);
workRow["CreateDate"] = myReader.GetValue(2);
dt.Rows.Add(workRow);
//Response.Write(myReader.GetValue(0).ToString()+"<br>");
}
myReader.Close();
//BindGrid();
MyDataGrid.DataSource = dt.DefaultView;// myReader;
MyDataGrid.DataBind();
//-------------------------------------------------------------------------------
//测试获得存储过程传出的参数
//SqlClient方式,测试通过
//-------------------------------------------------------------------------------
//连接数据库
SqlConnection sqlconn=new SqlConnection();
sqlconn.ConnectionString="server=192.168.3.80;uid=sa;Password=rain;database=MyStudy";
//server=192.168.3.80;uid=sa;Password=rain;database=MyStudy";
sqlconn.Open();
SqlCommand com=new SqlCommand();
com.Connection=sqlconn;
com.CommandText="ShowCmdTest";
com.CommandType=CommandType.StoredProcedure;
SqlParameter[] p_htno=new SqlParameter[2];
p_htno[0] = new SqlParameter("@inA",SqlDbType.VarChar,80);
p_htno[1] = new SqlParameter("@outB",SqlDbType.VarChar,80);
p_htno[0].Value ="测试输入";
p_htno[1].Direction=ParameterDirection.Output;
//p_htno[1].Value = this.drop_character.Items[this.drop_character.SelectedIndex].Value;
for ( int i =0 ;i< p_htno.Length;i++)
{
com.Parameters.Add(p_htno[i]);
}
com.ExecuteNonQuery();
Response.Write(p_htno[1].Value.ToString()+"<br>测试输出成功!");