简体   繁体   English

如何通过SQL查询返回字符串值?

[英]How to return string value through a sql query?

I have a query which retuns a string value as the output. 我有一个查询,该查询将字符串值作为输出调整。 But shows an error in the return. 但是显示返回错误。 I dont know how to fix it. 我不知道如何解决。 Here i have added my query. 在这里,我添加了我的查询。

public string detailsRemarksGet(string ddlValue)
{

     string strQuery = @"select r.remarks
                        from [A_MASTER] m, [A_REMARKS] r
                        where m.A_REF_NO=r.A_REF_NO 
                        and r.A_REF_NO='"+ ddlValue +"' and DEPT='POS' ";
     return SqlHelper.ExecuteScalar(strConnStringAppeal, CommandType.Text, strQuery);
}

Here is mky code of .cs 这是.cs的mky代码

public string detailsRemarks(string ddlValue)
{
   string remarks= db.detailsRemarksGet(ddlValue);
   return remarks;
}

In the error list i get a message as follows 在错误列表中,我收到一条消息,如下所示

Error 2 Cannot implicitly convert type 'object' to 'string'. 错误2无法将类型'object'隐式转换为'string'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

Sql query return object type, you need to cast that as string . SQL查询返回object类型,您需要将其强制转换为string So cast as string using ToString() method in return statement 因此,在return语句中使用ToString()方法转换为string

return SqlHelper.ExecuteScalar(strConnStringAppeal, CommandType.Text, strQuery).ToString();

Just convert your return type to String , because your method name db.detailsRemarksGet will return you an object and your detailsRemarks method return type is string. 只需您的返回类型转换 为String即可 ,因为您的方法名称db.detailsRemarksGet将返回一个对象,而detailsRemarks方法的返回类型为string。

public string detailsRemarks(string ddlValue)
{
    string remarks= Convert.ToString(db.detailsRemarksGet(ddlValue));
    return remarks;
}

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

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