C/Visual C - Ayuda enviando datagridview C# a una tabla existente en SQL.

 
Vista:
sin imagen de perfil

Ayuda enviando datagridview C# a una tabla existente en SQL.

Publicado por Alexander Miranda (1 intervención) el 10/12/2017 20:59:37
Saludos;

Amigos, solicito una ayuda, he buscado y no doy como debo enviar un DatagridView ya cargado en C# y enviarlo a una tabla existente en SQL por medio de un botón.
agrego el código de importación desde cualquier archivo en excel (también le puede servir a alguien.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
 
 
 
 
namespace ImportarExcelToDatagridview
{
    class Importar
    {
        OleDbConnection conn;
        OleDbDataAdapter MyDataAdapter;
        DataTable dt;
 
        public void importarExcel(DataGridView dgv,String hoja1)
        {
            String ruta = "";
            try
            {
                OpenFileDialog openfile1 = new OpenFileDialog();
                openfile1.Filter = "Excel Files |*.xlsx";
                openfile1.Title = "Seleccione el archivo de Excel";
                if (openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (openfile1.FileName.Equals("") == false)
                    {
                        ruta = openfile1.FileName;
                    }
                }
 
                    conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + ruta + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'");
                    MyDataAdapter = new OleDbDataAdapter("Select * from [" + hoja1 + "$]", conn);
                    dt = new DataTable();
                    MyDataAdapter.Fill(dt);
                    dgv.DataSource = dt;
 
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}



y el avance que tengo de la importación es el siguiente(me debe faltar nada)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
 
 
 
namespace ImportarExcelToDatagridview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            new Importar().importarExcel(dataGridView1, "hoja1");
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conexion = new SqlConnection("Data Source=DESKTOP-2DDA7JF;Initial Catalog=contratos;Persist Security Info=True;User ID=retardos;Password=123456789");
            conexion.Open();
            SqlBulkCopy exportar = default(SqlBulkCopy);
            exportar = new SqlBulkCopy(conexion);
            exportar.DestinationTableName = "maes_empl";
            //exportar.WriteToServer();
 
            conexion.Close();
            MessageBox.Show("exportacion Exitosa!");
 
        }
    }
}


agradezco de su amable colaboración. Feliz día!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder