C #. Use sql data to do access searches

-1

Bunas. I would like to know how I can extract data from sql and use each extracted data to do a search in a database in access. This is my code.

public DataTable heater()
    {
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        OleDbDataAdapter da1 = new OleDbDataAdapter();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();

        SqlConnection con = new SqlConnection(consql);
        con.Open();
        da.SelectCommand = new SqlCommand("SELECT spmh.[Samtec_Part_Master_Description] FROM[production].[Pockets].[PocketHeater] as ph inner join[production].[dbo].[SAMTEC_PART_MASTER] as spm on ph.[PocketPartNumberId] = spm.[Samtec_Part_Master_ID] inner join[production].[dbo].[SAMTEC_PART_MASTER] as spmh on ph.[HeaterPartNumberId] = spmh.[Samtec_Part_Master_ID] WHERE spm.[Samtec_Part_Master_Description] = '" + extraer() + "' or spm.[Samtec_Part_Master_Description] = '" + prt + "' ", con);

        da.Fill(ds);

        dt = ds.Tables[0];


        OleDbConnection cone = new OleDbConnection(conaccess);
        cone.Open();
        da1.SelectCommand = new OleDbCommand("SELECT Loc_Heater FROM Heater Where Part_number_heater = '" + dt + "' ", cone);

        da1.Fill(ds1);

        dt1 = ds.Tables[0];

        return dt1;
    }

I think it can be stored in an array but I do not know how to do it, if someone can help me. Thanks

    
asked by use2105 28.11.2016 в 16:22
source

1 answer

0

Basically the query of SQL that you have to do is to manage it in a Reader so that in this way you can iterate every row of the obtained result:

using (var conn = new SqlConnection(consql))
{
    using (var cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "SELECT spmh.[Samtec_Part_Master_Description] FROM[production].[Pockets].[PocketHeater] as ph inner join[production].[dbo].[SAMTEC_PART_MASTER] as spm on ph.[PocketPartNumberId] = spm.[Samtec_Part_Master_ID] inner join[production].[dbo].[SAMTEC_PART_MASTER] as spmh on ph.[HeaterPartNumberId] = spmh.[Samtec_Part_Master_ID] WHERE spm.[Samtec_Part_Master_Description] = '" + extraer() + "' or spm.[Samtec_Part_Master_Description] = '" + prt + "' ";
        conn.Open();
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                try
                {

                    using(var cone  = new OleDbConnection(conaccess)){
                        da1.SelectCommand = new OleDbCommand("SELECT Loc_Heater FROM Heater Where Part_number_heater = '" + reader["Samtec_Part_Master_Description"].ToString() + "'");
                        //Manejo del resultado de la ejecución de la consulta a MS-Access
                    }
                }
                catch (Exception ex) {
                    //Manejo de la excepción en caso de falla
                }
            }
        }
    }
}
    
answered by 28.11.2016 в 16:44