CREATE PROC P_TEST @Name VARCHAR() @Rowcount INT OUTPUT AS BEGIN SELECT * FROM T_Customer WHERE NAME=@Name SET @Rowcount=@@ROWCOUNT END GO
存储过程调用如下:
DECLARE @i INT EXEC P_TEST A@i OUTPUT SELECT @i 结果
/* Name Address Tel
A Address Telphone (所影响的行数为 行) (所影响的行数为 行)
*/ DotNet 部分(C#) WebConfig 文件:
C#代码:(用到两个测试控件DataGrid(用于显示绑定结果集合)Lable(用于显示存储过程返回单值)
//添加数据库引用
代码
using SystemDataSqlClient;
private void Page_Load(object sender SystemEventArgs e)
{ // 在此处放置用户代码以初始化页面
String DBConnStr; DataSet MyDataSet=new DataSet();
SystemDataSqlClientSqlDataAdapter DataAdapter=new SystemDataSqlClientSqlDataAdapter();
DBConnStr=SystemConfigurationConfigurationSettingsAppSettings[ConnectString];
SystemDataSqlClientSqlConnection myConnection = new SystemDataSqlClientSqlConnection(DBConnStr);
if (myConnectionState!=ConnectionStateOpen)
{ myConnectionOpen(); }
SystemDataSqlClientSqlCommand myCommand = new SystemDataSqlClientSqlCommand(P_TestmyConnection);
myCommandCommandType=CommandTypeStoredProcedure; //添加输入查询参数赋予值
myCommandParametersAdd(@NameSqlDbTypeVarChar);
myCommandParameters[@Name]Value =A; //添加输出参数
myCommandParametersAdd(@RowcountSqlDbTypeInt);
myCommandParameters[@Rowcount]Direction=ParameterDirectionOutput;
myCommandExecuteNonQuery();
DataAdapterSelectCommand = myCommand;
if (MyDataSet!=null)
{ DataAdapterFill(MyDataSettable); }
DataGridDataSource=MyDataSet; DataGridDataBind(); //得到存储过程输出参数
LabelText=myCommandParameters[@Rowcount]ValueToString();
if (myConnectionState == ConnectionStateOpen) { myConnectionClose();
}
}
运行以上代码即可(返回记录集合和存储过程返回值)