日期:2014-05-18  浏览次数:20769 次

如何用c#用这个存储过程?
CREATE PROCEDURE [dbo].[protest] 
@Field varchar(50)='',
@tmpStr varchar(50)='',
@TableName nvarchar(1000)
AS
declare @sql nvarchar(4000)
set @sql=N'SELECT TOP 1 @tmpStr='+@Field+' FROM '+@TableName
exec sp_executesql @sql,N'@tmpStr 类型 out',@tmpStr out



如何用c#用这个存储过程?

------解决方案--------------------
sf
------解决方案--------------------
try
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["csDB"].ConnectionString;);
conn.Open();
SqlCommand scmd = new SqlCommand("protest",conn);
scmd.CommandType = CommandType.StoredProcedure;
scmd.Parameters.Add("@Field", SqlDbType.Varchar,50).Value = strField;
string tmpStr = string.Empty;
scmd.Parameters.Add("@tmpStr", SqlDbType.Varchar,50).Direction = ParameterDirection.Output;
scmd.Parameters.Add("@TableName", SqlDbType.NVarchar,1000).Value = tableName;
sdr = scmd.ExecuteReader(CommandBehavior.CloseConnection);
if(sdr.Read())
{
tmpStr = scmd.Parameters["@tmpStr"].Value;
}
}catch(SqlException sqx)
{
//...........................
}

------解决方案--------------------
对不起, 不是用:
sdr = scmd.ExecuteReader(CommandBehavior.CloseConnection); 
if(sdr.Read()) 

tmpStr = scmd.Parameters["@tmpStr"].Value; 

改为:
int row_affected = scmd.ExecuteNoQuery();
if(row_affected >0)
{
 tmpStr = scmd.Parameters["@tmpStr"].Value.ToString();
}
------解决方案--------------------
http://blog.54master.com/index.php/586951/viewspace-19909