Frage

I am new to database and SQL, so i have no idea what to initialize at dat_set

My code:

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;
}
War es hilfreich?

Lösung

You have to initialize your DataSet before calling Fill method.

 DataSet dat_set = new DataSet();

Example: Not tested code

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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top