我无法调用以下存储过程。当我调用ExecuteReader时,我收到错误'GetAverages2附近的语法不正确'。我可以创建存储过程并从TSQL调用它。我似乎无法让它在ADO中运行:
CREATE PROCEDURE GetAverages2
@CompanySize INT,
@Q1 FLOAT OUT,
@Q2 FLOAT OUT
AS
SELECT @Q1 = 1, @Q2 = 2
GO
string connectionString = ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand("GetAverages2", connection))
{
command.Parameters.Add("@CompanySize", System.Data.SqlDbType.Int).Value = int.Parse(Request.QueryString["CompanySizeId"]);
command.Parameters.Add("@Q1", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add("@Q2", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
答案 0 :(得分:2)
SqlCommand.CommandType默认为CommandType.Text,因此它尝试将文本“GetAverages2”作为原始SQL语句执行。在创建命令后添加此行:
command.CommandType = CommandType.StoredProcedure;