System.data.dataset出现未知错误

时间:2014-05-07 05:47:29

标签: c# sql dataset database-connection

我是数据库和SQL的新手,所以我不知道在dat_set初始化什么

我的代码:

public System.Data.DataSet GetConnection
{
        get { return MyDataSet(); }
}

private System.Data.DataSet MyDataSet()
{
    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
    con.Open();
    da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
    da_1.Fill(dat_set);   
    con.Close();
    return dat_set;
}

1 个答案:

答案 0 :(得分:4)

在调用Fill方法之前,您必须初始化DataSet

 DataSet dat_set = new DataSet();

示例:未经过测试的代码

private System.Data.DataSet MyDataSet()
{

     using (SqlConnection connection = new SqlConnection(strCon))
        {
            //Create a SqlDataAdapter 
            SqlDataAdapter adapter = new SqlDataAdapter();

            // Open the connection.
            connection.Open();

            SqlCommand command = new SqlCommand(sql_string, connection);
            command.CommandType = CommandType.Text;

            // Set the SqlDataAdapter's SelectCommand.
            adapter.SelectCommand = command;

            // Fill the DataSet.
            System.Data.DataSet dataSet = new System.Data.DataSet();
            adapter.Fill(dataSet);

            // Close the connection.
            connection.Close();

            return dataSet;
       }

     return default(System.Data.DataSet);
}