用c#读取存储过程的输出值create proc RidSel
@single char(10) output,
@double char(10) output,
@business char(10) output,
@president char(10) output
--以上输出参数
as
select @single=Rid from rinfo where Rin='0'and Rtype='标准单人间'
select @double=Rid from rinfo where Rin='0'and Rtype='标准双人间'
select @business=Rid from rinfo where Rin='0'and Rtype='商务套房'
select @president=Rid from rinfo where Rin='0'and Rtype='总统套房'
--这里能一次性写四行查询语句吗?
GO
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=hotel;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("exec RidSel @single,@double ,@business ,@president", con);//执行存储过程并传递输出参数
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
//这里显示每条记录的值
}结果dr.read()没有返回任何值,这是怎么回事呢?难道是存储过程写的有问题
@single char(10) output,
@double char(10) output,
@business char(10) output,
@president char(10) output
--以上输出参数
as
select @single=Rid from rinfo where Rin='0'and Rtype='标准单人间'
select @double=Rid from rinfo where Rin='0'and Rtype='标准双人间'
select @business=Rid from rinfo where Rin='0'and Rtype='商务套房'
select @president=Rid from rinfo where Rin='0'and Rtype='总统套房'
--这里能一次性写四行查询语句吗?
GO
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=hotel;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("exec RidSel @single,@double ,@business ,@president", con);//执行存储过程并传递输出参数
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
//这里显示每条记录的值
}结果dr.read()没有返回任何值,这是怎么回事呢?难道是存储过程写的有问题
SqlCommand cmd = new SqlCommand("Rid");
cmd.CommandType = CommandType.StoredProcedure;
var single = new SqlParameter("@single", SqlDbType.Char, 10, ParameterDirection.OutPut);
cmd.SqlParameters.Add(single);
...
cmd.ExecutNonQuery();
result = Convert.ToString(single.Value);
红色的两个属性很重要
SqlParameter single = new SqlParameter("@single",SqlDbType.Char,10,ParameterDirection.Output);这一句报错提示“错误 与“System.Data.SqlClient.SqlParameter.SqlParameter(string, System.Data.SqlDbType, int, string)”最匹配的重载方法具有一些无效参数 D:\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs 48 34 WindowsApplication2
”
SqlCommand cmd = new SqlCommand("RidSel", con);
cmd.CommandType = CommandType.StoredProcedure;
//
SqlParameter single = new SqlParameter("@single",SqlDbType.Char,10);
SqlParameter doub = new SqlParameter("@double", SqlDbType.Char, 10);
SqlParameter business = new SqlParameter("@business", SqlDbType.Char, 10);
SqlParameter president = new SqlParameter("@president", SqlDbType.Char, 10);
single.Direction = ParameterDirection.Output;
doub.Direction = ParameterDirection.Output;
business.Direction = ParameterDirection.Output;
president.Direction = ParameterDirection.Output;
cmd.Parameters.Add(single);
cmd.Parameters.Add(doub);
cmd.Parameters.Add(business);
cmd.Parameters.Add(president);
cmd.ExecuteNonQuery();
string sing = single.Value.ToString();//single.Value有多个值,只能读取最后一个??这样写终于能读出来了,但问题是比如我数据库里single有多个值,怎么样都读取出来呢??
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(.....)DataReader dr = cmd.ExecuteReader();
do
{
while (rdr.Read())
{
}
}while (rdr.NextResult());