c# how do you return dataset from sqldatareader?

前端 未结 6 1235
[愿得一人]
[愿得一人] 2020-12-29 05:13

I have this in a public class:

SqlConnection myConnection = new SqlConnection(\"Data Source=hermes;database=qcvalues; Integrated Security=SSPI;\");
myConnect         


        
6条回答
  •  有刺的猬
    2020-12-29 05:39

    Instead of returning a SqlDataReader, you can change your code so that it returns a DataSet.

    SqlConnection myConnection = new SqlConnection("Data Source=hermes;database=qcvalues; Integrated Security=SSPI;");
    DataSet dst = new DataSet();
    SqlDataAdapter dap = new SqlDataAdapter(InitializeQuery(), mConnection);
    dap.Fill(dst, "DataSetName");
    

    One of the neat things about this approach is that Fill opens and closes the database connection for you.

提交回复
热议问题