Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该如何设计存储过程和c#方法来获得这样的结果_C#_Sql Server - Fatal编程技术网

我应该如何设计存储过程和c#方法来获得这样的结果

我应该如何设计存储过程和c#方法来获得这样的结果,c#,sql-server,C#,Sql Server,我这里有个问题 在c#代码中,我构造了一个名为 saveImages(Guid-ImageId,String-url,byte[]imageData)调用存储过程将数据存储在sql server上 我了解如何构造存储过程并从c#代码调用它的所有过程 我的问题是,现在我希望代码的行为方式是,如果save成功返回imageId,否则返回null 我应该如何构造存储过程?如何设置参数? 那么如何获得C#中的返回值呢 谢谢如前所述,您可以从添加以下内容开始: SELECT SCOPE_IDENTITY(

我这里有个问题

在c#代码中,我构造了一个名为
saveImages(Guid-ImageId,String-url,byte[]imageData)
调用存储过程将数据存储在sql server上

我了解如何构造存储过程并从c#代码调用它的所有过程

我的问题是,现在我希望代码的行为方式是,如果save成功返回imageId,否则返回null

我应该如何构造存储过程?如何设置参数? 那么如何获得C#中的返回值呢


谢谢

如前所述,您可以从添加以下内容开始:

SELECT SCOPE_IDENTITY();
在存储过程的末尾

您的方法应该如下所示:

public int saveImages(Guid ImageId, String url, byte[] imageData)
    {
      using (SqlCommand command = new SqlCommand() {
        Connection = your connection,
        CommandType = CommandType.StoredProcedure,
        CommandText = "your Stored Procedure"
      })
      {
        try
        {
          command.Parameters.AddWithValue(...); // Add your Parameters here
          return (int)command.ExecuteScalar();
        }
        catch
        {
          return 0;
        }
      }
    }

到目前为止,您到达了哪里,请告诉我们。如果您将存储过程的调用放在try-catch块中,那么如果insert语句失败,它将被捕获,并且您可以通过返回null来处理错误,如果没有错误发生,则返回通过添加从存储过程获得的值;选择SCOPE_IDENTITY();在存储过程的insert语句结束时,我注意到前面的两个问题已结束,这也将结束。在提问时,请使用。你说你“了解一切”,但你没有提供任何东西,也没有提出非常广泛的问题;“我应该如何构造存储过程”。如果您没有尝试过任何东西,请告诉我们为什么不尝试,以及您搜索过的内容和遇到的问题。例如,您搜索过“存储过程参数c#”吗?很抱歉,后续操作太晚了。我最近正忙于别的事情。你们的建议很好,我会努力提高我的提问技巧。谢谢大家。