【问题标题】:Return String from a webmethod instead of a dataset.从 web 方法而不是数据集返回字符串。
【发布时间】:2009-01-14 15:33:43
【问题描述】:

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

[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);
}

【问题讨论】:

    标签: c# .net web-services


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      这会将数据作为 XML 返回。

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

      【讨论】:

      • 这种情况下的值只是返回一个单一的值,你能给出一个完整的例子吗?
      • 这应该可以工作:ds.Tables[0].Rows[0][0].ToString();虽然我建议你使用存储过程,并使用输出参数来返回结果。
      【解决方案3】:
      //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();
      

      【讨论】:

        【解决方案4】:

        这是我完成并正在工作的内容,感谢您的意见:

        [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();
            }
        

        【讨论】:

          【解决方案5】:

          @MartGrif

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

          [WebMethod]
          public String GetPONumber(string Database)
          {   
              //Create Object ready for Value
              object po = "";
          
              //Set Connection
              using(SqlConnection connection = new SqlConnection(GetConnString(Database)))
              {
                  string Query = @" SQL QUERY GOES HERE!!!! ";
                  using(SqlCommand command = new SqlCommand(Query, connection))
                  {
                      try
                      {
                          connection.Open();
                          po = Command.ExecuteScalar();
                      }
                      catch
                      {
                          //Error
                      }
                  }
              }
              return po.ToString();
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-11
            • 1970-01-01
            • 2018-11-16
            • 1970-01-01
            相关资源
            最近更新 更多