简体   繁体   中英

How to do a search in Access database using C#?

I do not know if I'm doing it right, I would like help. I need to query for "n_tp" the records for txbCnpj.Text.

My Access

在此处输入图片说明

My code c# (Honestly I do not know how to do)

private void button1_Click_1(object sender, EventArgs e)
    {
        string strsql, strconc;
        OleDbCommand objCommand = null;
        OleDbConnection objConnect = null;
        strconc = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\Program Files\\TPMicroVix\\reg_tp.accdb;";
        objConnect = new OleDbConnection(strconc);

        objConnect.Open();

        OleDbCommand Cmm = new OleDbCommand();

        Cmm.CommandText = "SELECT cnpj, nome_cl, portal, empresa, email, tel, canal, detalhes FROM reg_tp WHERE n_tp = ?;";
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = objConnect;

        OleDbDataReader DR;
        DR = Cmm.ExecuteReader();

        txbCnpj.Text = DR.GetString(0);
}

I was able to solve

string strconc;

            OleDbConnection objConnect = null;
            strconc = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\Program Files\\TPMicroVix\\reg_tp.accdb;";
            objConnect = new OleDbConnection(strconc);

            objConnect.Open();



            OleDbCommand cmm = new OleDbCommand("SELECT cnpj, nome_cl, portal, empresa, email, tel, canal, detalhe FROM tb_tp WHERE n_tp = '" + txbTp.Text + "';", objConnect);


            cmm.Parameters.Add("@n_tp", OleDbType.VarChar).Value = txbTp.Text;


            cmm.CommandType = CommandType.Text;

            OleDbDataReader reader;
            reader = cmm.ExecuteReader();
            reader.Read();




            txbCnpj.Text = reader.GetString(0);
            txbNome.Text = reader.GetString(1);
            txbPortal.Text = reader.GetString(2);
            txbEmpresa.Text = reader.GetString(3);
            txbEmail.Text = reader.GetString(4);
            txbTel.Text = reader.GetString(5);
            cboCanal.Text = reader.GetString(6);
            txbDetalhe.Text = reader.GetString(7);


            objConnect.Close();

your code mostly looks correct. I am just going to refactor it for you by applying best practices.

private void button1_Click_1(object sender, EventArgs e)
{
     var result = GetData();
     // do whatever with your data
}


private IList<ResultObject> GetData()
{
    IList<ResultObject> result = new List<ResultObject>();

    string strconc = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\Program Files\\TPMicroVix\\reg_tp.accdb;";
    using (OleDbConnection objConnect = new OleDbConnection(strconc))
    using (OleDbCommand Cmm = new OleDbCommand())
    {
        objConnect.Open();
        Cmm.CommandText = "SELECT cnpj, nome_cl, portal, empresa, email, tel, canal, detalhes FROM reg_tp WHERE n_tp = ?;";
        Cmm.Parameters.AddWithValue("tpParam", "somevalue");
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = objConnect;

        using (OleDbDataReader DR = Cmm.ExecuteReader())
        {
              ResultObject obj = new ResultObject
              {
                   Prop1 = DR.GetString("cnpj"),
                   Prop2 = DR.GetString("nome_cl"),
                   ....
              }
              result.Add(obj);
        }

    }

    return result;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM