从存储过程中获取字符串类型值 [英] To get string type value from store procedure

查看:117
本文介绍了从存储过程中获取字符串类型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在SQL SERVER中创建一个存储过程,该存储过程将返回我的字符串值,并且还希望在C#中创建方法,该方法将执行此存储过程并读取字符串值并将其存储在变量中.



请以最佳方法帮助我.

Hello All,

I want to create one stored procedure in SQL SERVER which will return me string value and also want to create method in C# which will execute this stored procedure and read the string value and stored it in variable.



Please help me with the best approach to do this.

推荐答案

问这个问题的人已经指定了您需要的代码:

http://stackoverflow.com/questions/706361/getting-在c-sharp中返回存储过程中的返回值 [
The person asking this question has specified the code you need:

http://stackoverflow.com/questions/706361/getting-return-value-from-stored-procedure-in-c-sharp[^]

Just make sure to apply the fix specified in the answers. That will allow you to execute a stored procedure in C#, get the return value, and store it in a variable.


hi,
请尝试此操作对您有帮助....



Please try this it help you....


public string ExecuteDataSetSP(string spName, Boolean withOutPara)
  {

      SqlDataAdapter daDataAdapter = new SqlDataAdapter();
      DataSet dsDataSet = new DataSet();

      // Initialize the Exception ...

          // Prepare DataAdapter ...
          daDataAdapter.SelectCommand = new SqlCommand();
          daDataAdapter.SelectCommand.Connection = cnnConnection;
          daDataAdapter.SelectCommand.CommandText = spName;
          daDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
          // Pass Parameters ...


              // Add output parameters ...
              daDataAdapter.SelectCommand.Parameters.Add("@o_ErrorMesg", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;

          // Open Database Connection ...
          cnnConnection.Open();
          // Fill the DataSet ...
          daDataAdapter.Fill(dsDataSet);
          // Read values of Output Parameters

              // Read Values of Output Paramters and Stored into Property
              mErrorMsg = (string)(daDataAdapter.SelectCommand.Parameters["@o_ErrorMesg"].Value);


      return mErrorMsg ;
  }



您的存储过程包含返回字符串的Output参数.



Your stored procedure contain Output parameter which return string.




有两种方法可以解决此问题.

1.从存储过程返回输出参数.
http://www.c -sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/ [
Hi,

There is two ways to solve this problem.

1. Return Output Parameter from Stored Procedure.
http://www.c-sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/[^]

2. Return table from Stored Procedure which will contain only one row and column
as your string data.
//Suppose your command object is cmd and dataset object is dt
SqlDataAdapter sda=new SqlDataAdapter(cmd);
sda.Fill(ds);
string strMyVal=ds.Tables[0].Rows[0][0].toString();
//now strMyVal is ready with your string value returned from database.




一切顺利..
--AK




All the best..
--AK


这篇关于从存储过程中获取字符串类型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆