Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: hpereira en 28 Noviembre 2013, 17:59 pm



Título: Leer archivos dbf de fox en c# windows from
Publicado por: hpereira en 28 Noviembre 2013, 17:59 pm
Buenos días amigos tengo este problema:

Como puedo leer varios archivos dbf c# y luego filtrar u buscar informacion entre ellos para luego mostralo en un grid actualmente hago esto y me funciona:

Este codigo me funciona pero como ven tengo que seleccionar el archivo y me lo muestra en un grid...

Gracias
Jose Pereira

Código
  1. private void button1_Click(object sender, EventArgs e)
  2.        {
  3.            OpenFileDialog fdlg = new OpenFileDialog();
  4.            fdlg.Title = "Seleccione un Archivo";
  5.           // fdlg.InitialDirectory = @"C:\FoxPro\CAJ&BAN\Dbf005\C&B0015.DBF\";
  6.           // fdlg.FileName = @"C:\FoxPro\NCTAXPAG\dbf002\CXP_107A.DBF";
  7.            fdlg.FileName = @"C:\FoxPro\NCTAXPAG\dbf002\CXP_FILE.DBF";
  8.            //fdlg.FileName = txtFileName.Text;
  9.            fdlg.Filter = "DBF Files(*.dbf)|CXP_ARCH.DBF";
  10.            //fdlg.Filter = "DBF Files(*.dbf)|C&B0015.DBF|All Files(*.*)|*.*";
  11.            fdlg.FilterIndex = 1;
  12.            fdlg.RestoreDirectory = true;
  13.            if (fdlg.ShowDialog() == DialogResult.OK)
  14.            {
  15.                txtFileName.Text = fdlg.FileName;
  16.                Import();
  17.                Application.DoEvents();
  18.            }
  19.        }
  20.  
  21. private void Import()
  22.        {
  23.            if (txtFileName.Text.Trim() != string.Empty)
  24.            {
  25.                try
  26.                {
  27.                    DataTable dt = GetDataTableDBF(txtFileName.Text);
  28.                    Dgvlista.DataSource = dt.DefaultView;
  29.  
  30.                }
  31.                catch (Exception ex)
  32.                {
  33.                    MessageBox.Show(ex.Message.ToString());
  34.                }
  35.            }
  36.        }
  37.  
  38. public static DataTable GetDataTableDBF(string strFileName)
  39.        {
  40.            System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + System.IO.Path.GetFullPath(strFileName).Replace(System.IO.Path.GetFileName(strFileName), "") + ";Exclusive=No");
  41.            conn.Open();
  42.            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
  43.            System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
  44.            System.Data.DataSet ds = new System.Data.DataSet();
  45.            adapter.Fill(ds);
  46.            return ds.Tables[0];
  47.        }
  48.  
  49.  



[MOD]: Hay unas reglas que seguir, cualquier (siguiente) mensaje que no utilice las etiquetas de código podrá ser eliminado sin previo aviso.