Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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# 从webmethod而不是dataset返回字符串。_C#_.net_Web Services - Fatal编程技术网

C# 从webmethod而不是dataset返回字符串。

C# 从webmethod而不是dataset返回字符串。,c#,.net,web-services,C#,.net,Web Services,我如何重构它以使其返回字符串而不是数据集 [WebMethod] public DataSet GetPONumber(string Database) { SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = GetConnString(Database); // build query string strSQL = @" A SELECT QUERY!!!!! ";

我如何重构它以使其返回字符串而不是数据集

[WebMethod]
public DataSet GetPONumber(string Database)
{
    SqlConnection sqlConn = new SqlConnection();

    sqlConn.ConnectionString = GetConnString(Database);

    // build query
    string strSQL = @" A SELECT QUERY!!!!! ";

    SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlConn);
    DataSet ds = new DataSet();
    da.Fill(ds, "NEWPO");

    return (ds);
}

您可以将数据集转换为其JSON字符串表示形式。这样,基本上任何客户机都可以轻松地使用它。

您可以将数据集转换为其JSON字符串表示形式。这样,基本上任何客户端都可以轻松地使用它。

您可以返回ds.GetXml()并更改返回类型

这将以XML的形式返回数据

如果结果非常简单(比如一个值),您可能只想直接返回它们。

您可以返回ds.GetXml()并更改返回类型

//Use an SqlCommand and the ExecuteScalar method.
//Cast returnValue to known object.
SqlCommand command = sqlConn.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = @" A SELECT QUERY!!!!! ";
sqlConn.Open();
object returnValue = command.ExecuteScalar();
command.Dispose();
return returnValue.ToString();
这将以XML的形式返回数据


如果您的结果非常简单(比如说一个值),您可能只想直接返回它们。

这就是我使用的,并且正在工作,感谢您的输入:

//Use an SqlCommand and the ExecuteScalar method.
//Cast returnValue to known object.
SqlCommand command = sqlConn.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = @" A SELECT QUERY!!!!! ";
sqlConn.Open();
object returnValue = command.ExecuteScalar();
command.Dispose();
return returnValue.ToString();
[WebMethod]
    public String GetPONumber(string Database)
    {   
        //Create Object ready for Value
        object po = "";

        //Set Connection
        SqlConnection Connection = new SqlConnection(GetConnString(Database));

        //Open Connection
        Connection.Open();

        //Set Query to string
        string Query = @" SQL QUERY GOES HERE!!!! ";

        //Run Query
        SqlCommand Command = new SqlCommand(Query, Connection);

        //Set Value from Query
        try
        {
            po = Command.ExecuteScalar();
        }
        catch
        {
            //Error
        }

        //Clean up sql
        Command.Dispose();
        Command = null;


        //Clean up connection
        Connection.Close();
        Connection.Dispose();
        Connection = null;

        //Return Value
        return po.ToString();
    }

这就是我的工作,谢谢你的意见:

[WebMethod]
    public String GetPONumber(string Database)
    {   
        //Create Object ready for Value
        object po = "";

        //Set Connection
        SqlConnection Connection = new SqlConnection(GetConnString(Database));

        //Open Connection
        Connection.Open();

        //Set Query to string
        string Query = @" SQL QUERY GOES HERE!!!! ";

        //Run Query
        SqlCommand Command = new SqlCommand(Query, Connection);

        //Set Value from Query
        try
        {
            po = Command.ExecuteScalar();
        }
        catch
        {
            //Error
        }

        //Clean up sql
        Command.Dispose();
        Command = null;


        //Clean up connection
        Connection.Close();
        Connection.Dispose();
        Connection = null;

        //Return Value
        return po.ToString();
    }
@马格里夫

我已经修改了您的代码以包含using语句,这是常见用法。它还使代码更简洁,我相信,更可读。using语句在代码块末尾自动处理对象。请参阅有关的文档

@马格里夫

我已经修改了您的代码以包含using语句,这是常见用法。它还使代码更简洁,我相信,更可读。using语句在代码块末尾自动处理对象。请参阅有关的文档


本例中的值仅返回一个值,能否显示一个完整的示例?这应该有效:ds.Tables[0]。Rows[0][0]。ToString();虽然我建议您使用存储过程并使用输出参数来返回结果。在本例中,该值仅返回一个值,但您能否给出一个完整的示例?这应该有效:ds.Tables[0]。Rows[0][0]。ToString();尽管我建议您使用存储过程并使用输出参数来返回结果。