下面的代码是梦幻般的。但它能变得更紧凑和更简洁吗?特别是我对两个问题有怀疑。
用它作为in参数填充fill变量不是很难看吗?
是否可以使代码更紧凑,而不是通过String
C.*

String
  connectionString = "...",
  sqlStatement = "select * from Test",
  output = "";

SqlDataAdapter adapter = new SqlDataAdapter(sqlStatement, connectionString);
DataSet dataSet = new DataSet("Contents");
adapter.Fill(dataSet, "Test");
StringWriter stringWriter=new StringWriter();
dataSet.WriteXml(new XmlTextWriter(stringWriter));
XmlDocument document = new XmlDocument();
output = stringWriter.ToString();
document.LoadXml(output);

最佳答案

我不知道如何使它更简洁,但您确实需要关闭适配器和stringwriter。我会使用using块。

   String
       connectionString = "...",
       sqlStatement = "select * from Test",
       output = "";

    DataSet dataSet = new DataSet("Contents");
    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlStatement, connectionString)){
        adapter.Fill(dataSet, "Test");}
    using (StringWriter stringWriter = new StringWriter()) {
      dataSet.WriteXml(new XmlTextWriter(stringWriter));
      output = stringWriter.ToString();
    };
    XmlDocument document = new XmlDocument();
    document.LoadXml(output);

10-05 22:37