简体   繁体   English

C#SQL存储过程(插入) - 传递参数和检索参数

[英]C# SQL stored procedure (which inserts) - pass parameters and retrieve parameter

I have a stored procedure on my server that inserts some parameters and returns the ID that was inserted. 我的服务器上有一个存储过程,它插入一些参数并返回插入的ID I am writing a form to do this easily but I cannot seem to get the parameter which is passed back. 我正在写一个表单来轻松完成这个,但我似乎无法获得传回的参数。

To save you doing a whole bunch of possibly pointless reading, it's probably better to just pay attention to my C# code and let me know what I need to do in order to pass parameters and get one in return. 为了节省你做一大堆可能毫无意义的阅读,最好只关注我的C#代码,让我知道我需要做什么才能传递参数并得到一个回报。

C# Default.aspx C#Default.aspx

connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionInfo"]);
sql = "aStoredProc";

command = new SqlCommand(sql, connection);
command.CommandType = CommandType.StoredProcedure;

command.Parameter.Add(new SqlParameter("@FirstName", SqlDbType.VarChar)).Value = sFirstname;
command.Parameter.Add(new SqlParameter("@SurName", SqlDbType.VarChar)).Value = sSurname;

connection.Open(); 
int ID = command.ExecuteNonQuery();
connection.Close();

SQL aStoredProc SQL aStoredProc

IF EXISTS(SELECT * FROM aTable WHERE ID = @ID)
  -- User exists, update details
  BEGIN
    BEGIN TRAN
      UPDATE aTable
        SET 
          FirstName = @FirstName,
          SurName = @SurName,
          LastUpdate = GetDate()
        WHERE ID = @ID

        IF (@@Error != 0)
          ROLLBACK TRAN
        ELSE
          COMMIT TRAN
        END
ELSE
  -- New user
  BEGIN
    BEGIN TRAN
      INSERT aTable (
        FirstName,
        SurName,
        GetDate()
      )
        VALUES (
          @FirstName, 
          @SurName,
          @LastUpdate
      ) 

      SELECT @ID = @@IDENTITY

      IF (@@Error != 0)
        ROLLBACK TRAN
      ELSE
        COMMIT TRAN
      END

The parameter @ID is listed in the stored proc as: 参数@ID在存储过程中列为:

@ID (int, Input/Output, No default)

and proc has 'Return integer'. 和proc有'返回整数'。 This used to work fine with a VBA solution prior to a SQL Server 2005 upgrade. 在SQL Server 2005升级之前,这曾经适用于VBA解决方案。

Thanks in advance. 提前致谢。

connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionInfo"]); 
sql = "aStoredProc"; 
command = new SqlCommand(sql, connection); 
command.CommandType = CommandType.StoredProcedure; 
command.Parameter.Add(new SqlParameter("@FirstName", SqlDbType.VarChar)).Value = sFirstname; 
command.Parameter.Add(new SqlParameter("@SurName", SqlDbType.VarChar)).Value = sSurname; 
command.Parameter.Add(new SqlParameter("@SurName", SqlDbType.VarChar)).Value = sSurname; 
SqlParameter ParamId = cmd.Parameters.Add( "@Id", SqlDbType.Int);
ParamId.Direction = ParameterDirection.InputOutput;
command.Parameter.Add(ParamId);
connection.Open();  
command.ExecuteNonQuery(); 
int ID = ParamId.Value;
connection.Close();
  1. you have to add output paramter in Parameter collection. 你必须在参数集合中添加输出参数。
  2. Read Value like above. 读取值如上所述。

You have an error in your SQL, it should look like this: 您的SQL中有错误,它应该如下所示:

 INSERT aTable (FirstName,SurName,LastUpdate)
    VALUES (@FirstName, @SurName, GetDate() ) 

Not like this: 不是这样的:

 INSERT aTable (
    FirstName,
    SurName,
    GetDate()
  )
    VALUES (
      @FirstName, 
      @SurName,
      @LastUpdate
  ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将参数传递给c#中的sql存储过程 - Passing a parameter to an sql stored procedure in c# 从C#代码调用存储过程时,出现SQL错误“过程或函数需要参数,但未提供” - Sql error “Procedure or function expects parameter , which was not supplied” when calling a stored procedure from c# code 将C#方法参数映射到sql存储过程参数 - mapping c# method parameters to sql stored procedure parameters 无法将参数SQL日期类型从C#代码传递到存储过程 - Unable to pass parameter to the sql Date type to stored procedure from c# code Npgsql C# - 将参数作为复合类型数组传递到存储过程中 - Npgsql C# - pass parameters as array of composite type into stored procedure 在C#中运行存储过程,传递参数并捕获输出结果 - Run stored procedure in C#, pass parameters and capture the output result C#TSQL存储过程不传递参数? - C# TSQL Stored Procedure don't pass parameter? C#MVC数据类型传递给存储过程输出参数 - C# MVC Datatype to pass for stored procedure output parameter 如何将 C# class object 作为参数传递给存储过程? - How to pass C# class object as parameter to a stored procedure? 如何将C#数据表传递给mysql的存储过程参数 - How to pass C# datatable to mysql's stored procedure parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM