简体   繁体   English

获取从SQL Server返回的字符串值

[英]Get String Value Returned From SQL Server

I am using C# to run a SQL Server Stored Procedure, I want to store the returned variable from my sql stored procedure in a string variable, but every time I try to run it, my C# syntax errors telling me it is unable to convert varchar to int. 我使用C#运行SQL Server存储过程,我想将我的sql存储过程中返回的变量存储在字符串变量中,但每次我尝试运行它时,我的C#语法错误告诉我它无法转换varchar到int。 I am very new when it comes to communicating with C# and SQL so please forgive me if I omit needed information or am overlooking the most obvious points. 在与C#和SQL进行通信时我是新手,所以如果我省略了所需的信息或忽略了最明显的观点,请原谅我。 Below is my code, please informe me if any additional code is needed to further assist me in this issue. 以下是我的代码,如果需要任何其他代码来帮助我解决此问题,请告诉我。 Thank you in advance for all who assist 提前感谢所有帮助的人

//C# Syntax
private void btnBTNBTN_Click()
{
    string returnedvariable = DoThisStuff(abcd, faraca);
}

public string DoThisStuff(string abcd, int faraca)
{
    string value = string.Empty; 
    sqlsyntaxxxx = new StringBuilder();
    sqlsyntaxxxx.Append("exec dbo.AlphaDawg ");
    sqlsyntaxxxx.Append("'" + faraca + "'");
    _dataSet = RUNSQL(abcd, sqlsyntaxxxx.ToString());
    return value;
}

public DataSet RUNSQL(string abcd, string sqlsyntaxxxx)
{          
    _connectionString = System.Configuration.ConfigurationManager.AppSettings[connectionString].ToString();     
    _sqlDatabaseConnection = new SqlConnection(_connectionString);
    _sqlCommand = new SqlCommand(sqlsyntaxxxx, _sqlDatabaseConnection);
    _sqlDatabaseConnection.Open();
    _dataSet = new DataSet();
    _sqlDataAdapter = new SqlDataAdapter(_sqlCommand);            
    _sqlDataAdapter.Fill(_dataSet, "Data");
    return _dataSet;
}

SQL Stored Procedure SQL存储过程

ALTER PROCEDURE [dbo].[AlphaDawg]
(
    @faraca int
)
AS
BEGIN
    DECLARE @returnvalue varchar(500)

    UPDATE [dbo].[redllama]
    SET [AZYXIE] = '682134'+[zoneNumber]
    WHERE faraca = @faraca

    Set @returnvalue = (Select [AZYXIE] from [dbo].[redllama] WHERE faraca = @faraca)

    return @returnvalue
END

First, the SQL return statement can only return an integer from a procedure and that value isn't part of any dataset. 首先,SQL return语句只能从过程返回一个整数,并且该值不是任何数据集的一部分。

In any event, return is not what you want, even for integers 99% of the time (it's intended as a status value for other SQL procedures). 在任何情况下, return都不是你想要的,即使是99%的时间整数(它的目的是作为其他SQL过程的状态值)。 You usually return data through a dataset by executing a SELECT ... in your procedure. 您通常通过在过程中执行SELECT ...来返回数据集中的数据。 You can also return single values through an output parameter, but I would recommend the dataset until you are more familiar with this. 您也可以通过输出参数返回单个值,但我建议您使用数据集,直到您对此更加熟悉。

Change your SQL procedure as follows and it should work: 更改您的SQL过程如下,它应该工作:

ALTER PROCEDURE [dbo].[AlphaDawg]
(
    @faraca int
)
AS
BEGIN
    DECLARE @returnvalue varchar(500)

    UPDATE [dbo].[redllama]
    SET [AZYXIE] = '682134'+[zoneNumber]
    WHERE faraca = @faraca

    Select [AZYXIE] from [dbo].[redllama] WHERE faraca = @faraca)
END

Hmm, looks like your c# code needs to be changed a little bit also. 嗯,看起来你的c#代码也需要稍微更改一下。 I haven't done this in a while, but I think that this is what you want: 我有一段时间没有这样做,但我认为这就是你想要的:

public string DoThisStuff(string abcd, int faraca)
{
    string value = string.Empty; 
    sqlsyntaxxxx = new StringBuilder();
    sqlsyntaxxxx.Append("exec dbo.AlphaDawg ");
    sqlsyntaxxxx.Append("'" + faraca + "'");
    _dataSet = RUNSQL(abcd, sqlsyntaxxxx.ToString());

    value = _dataSet.Tables[0].Rows[0]["AZYXIE"].ToString();
    return value;
}

Your paramater variable faraca is defined as an int . 您的参数变量faraca定义为int So you need to change this line: 所以你需要改变这一行:

sqlsyntaxxxx.Append("'" + faraca + "'");

to

sqlsyntaxxxx.Append(faraca.ToString());

Also, you will have to set the value of value in your method DoThisStuff . 此外,您必须在方法DoThisStuff设置值的value As it is now... you are going to always just return an empty string. 就像现在一样......你将永远只返回一个空字符串。 So you need to pull a field out of your result set before the return statement... something like: 所以你需要在return语句之前从结果集中提取一个字段...类似于:

value = ds.Tables[0].Rows[0]["Myfield"]

Then change your stored procedure to: 然后将存储过程更改为:

ALTER PROCEDURE [dbo].[AlphaDawg]
(
    @faraca int
)
AS
BEGIN
    DECLARE @returnvalue varchar(500)

    UPDATE [dbo].[redllama]
    SET [AZYXIE] = '682134'+[zoneNumber]
    WHERE faraca = @faraca

    Select [AZYXIE] 
    from [dbo].[redllama] 
    WHERE faraca = @faraca

END

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM