C# 存储过程 输出参数不能返回 的问题?
时间:2011-05-12 来源:疯癫NET
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using HelloWinForm.DBUtility;
using System.Windows.Forms;
namespace HelloWinForm.DAL
{
class Student
{
SQLHelper sqlHelper = new SQLHelper();//非静态方法,需要实例化类
/// <summary>
/// 执行带输入输出参数的存储过程
/// </summary>
/// <returns></returns>
public int procInAndOut()
{
SqlParameter[] paramsArr =
{
sqlHelper.MakeOutParam("@outParam" , SqlDbType.Int , 4),
sqlHelper.MakeInParam("@inParam" , SqlDbType.NVarChar , 50 ,"tree")
};
paramsArr[0].Direction = ParameterDirection.Output;
int lines = sqlHelper.RunProc("proc_InAndOut", paramsArr); //return (int)cmd.Parameters[0].Value;
return lines;
}
/// <summary>
/// 执行带输入参数的存储过程,返回DataReader,不用此法,不方法,需要再次关闭数据连接
/// </summary>
/// <returns></returns>
public SqlDataReader getDataReader()
{
SqlDataReader reader;
SqlParameter[] paramsArr =
{
sqlHelper.MakeInParam("@StudentName" , SqlDbType.NVarChar , 50 ,"tree"),
sqlHelper.MakeInParam("@inParam" , SqlDbType.NVarChar , 50 ,"tree")
};
sqlHelper.RunProc("proc_getDataReader", paramsArr, out reader);
//sqlHelper.Close();//不能在此关闭
return reader;
}
/// <summary>
/// 如果使用DataReader应该在这里执行打开和关闭.
/// </summary>
/// <returns></returns>
public string getDataFromReader()
{
string returnStr = "";
SqlDataReader reader;
SqlParameter[] paramsArr =
{
sqlHelper.MakeInParam("@StudentName" , SqlDbType.NVarChar , 50 ,"tree"),
sqlHelper.MakeInParam("@inParam" , SqlDbType.NVarChar , 50 ,"tree")
};
sqlHelper.RunProc("proc_getDataReader", paramsArr, out reader);
try
{
while (reader.Read())
{
returnStr += reader.GetInt32(0);
returnStr += reader.GetString(1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlHelper.Close();
}
return returnStr;
}
public string test()
{
string str = "";
SqlDataReader dr = SQLHelper.ExecuteReader("select * from Student");
while (dr.Read())
{
str += dr["StudentNO"].ToString();
}
dr.Close();
return str;
}
public int proc()
{
return sqlHelper.RunProc("proc_Test");
}
}
}
相关阅读 更多 +