C# using statement catch error

后端 未结 16 1305
轻奢々
轻奢々 2021-01-30 14:06

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code:

 using (SqlComma         


        
16条回答
  •  借酒劲吻你
    2021-01-30 14:54

    Minor correction to the example: SqlDataAdapter also needs to be instantiated in a using statement:

    using (SqlConnection con = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))
    using (SqlCommand cmd = new SqlCommand(reportDataSource, con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
        cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
        cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
        con.Open();
    
        DataSet dset = new DataSet();
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            adapter.Fill(dset);
        }
        this.gridDataSource.DataSource = dset.Tables[0];
    }
    

提交回复
热议问题