Northwind数据库建存储过程: CREATE PROCEDURE QUERY_CUSTOMERS(@CITY NVARCHAR(50), @COUNT INT OUTPUT) AS BEGIN SELECT @COUNT = COUNT(1) FROM CUSTOMERS WHERE CITY = @CITY;
SELECT * FROM CUSTOMERS WHERE CITY = @CITY END 调用: SqlConnection connection = new SqlConnection(); string connStr = "server=.;uid=sa;pwd=sa;database=northwind"; connection.ConnectionString = connStr; connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "QUERY_CUSTOMERS"; SqlParameter param1 = new SqlParameter("@city", "London"); param1.Direction = ParameterDirection.Input; SqlParameter param2 = new SqlParameter("@count", 0); param2.Direction = ParameterDirection.Output; command.Parameters.Add(param1); command.Parameters.Add(param2); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable("Customers"); adapter.Fill(table); this.dataGridView1.AutoGenerateColumns = true; this.dataGridView1.DataSource = table; MessageBox.Show(command.Parameters[1].Value.ToString()); connection.Close();
CREATE PROCEDURE QUERY_CUSTOMERS(@CITY NVARCHAR(50), @COUNT INT OUTPUT)
AS
BEGIN
SELECT @COUNT = COUNT(1)
FROM CUSTOMERS
WHERE CITY = @CITY;
SELECT *
FROM CUSTOMERS
WHERE CITY = @CITY
END
调用:
SqlConnection connection = new SqlConnection();
string connStr = "server=.;uid=sa;pwd=sa;database=northwind";
connection.ConnectionString = connStr;
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "QUERY_CUSTOMERS";
SqlParameter param1 = new SqlParameter("@city", "London");
param1.Direction = ParameterDirection.Input;
SqlParameter param2 = new SqlParameter("@count", 0);
param2.Direction = ParameterDirection.Output;
command.Parameters.Add(param1);
command.Parameters.Add(param2);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable("Customers");
adapter.Fill(table);
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = table;
MessageBox.Show(command.Parameters[1].Value.ToString());
connection.Close();
objParams[X] = new SqlParameter("@Id", SqlDbType.Int);
objParams[X].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(...);
int result = (int)objParams[X].Value;
...
==============================
result即是你要的output值,参数类型根据你自己的来!