首先創建存儲過程,sql語句如下所示:
[sql]
USE [YCYFFJKXT]
GO
/****** 對象: StoredProcedure [dbo].[Login_UserCount] 腳本日期: 07/17/2012 14:53:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[Login_UserCount]
@ZH VARCHAR(20),
@MM varchar(50),
@Rowcount INT OUTPUT
AS
BEGIN
SELECT * FROM YHXX WHERE ZH=@ZH and MM=@MM
SET @Rowcount=@@ROWCOUNT
END
c#中調用
[csharp]
SqlConnection conn_Local = new SqlConnection();
conn_Local.ConnectionString = "server=.;database=YCYFFJKXT;user id=sa;password=sa";
try
{
conn_Local.Open();
SqlCommand cmd_Count = new SqlCommand("Login_UserCount",conn_Local);
cmd_Count.CommandType = CommandType.StoredProcedure;
//添加輸入查詢參數、賦予值
cmd_Count.Parameters.Add("@ZH", SqlDbType.VarChar);
cmd_Count.Parameters.Add("@MM",SqlDbType.VarChar);
cmd_Count.Parameters["@ZH"].Value = "A";
cmd_Count.Parameters["@MM"].Value = "B";
//添加輸出參數
cmd_Count.Parameters.Add("@Rowcount", SqlDbType.Int);
cmd_Count.Parameters["@Rowcount"].Direction = ParameterDirection.Output;
cmd_Count.ExecuteNonQuery();
Console.WriteLine("受影響的行數是"+cmd_Count.Parameters["@Rowcount"].Value.ToString());
}
catch (Exception ex)
{
www.2cto.com
throw ex;
}
finally
{
conn_Local.Close();
}
Console.ReadLine();
作者:pridescc