none
c# excel import sql update ?

    General discussion

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Data.Common;

    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }


            private void Form1_Load(object sender, EventArgs e)
            {

          }

            private void button1_Click(object sender, EventArgs e)
            {


                // Connection String to Excel Workbook
                string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+textBox1.Text+";Extended Properties=Excel 8.0" );
                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = excelConnectionString;
                OleDbCommand command = new OleDbCommand("select * from [sayfa1$]", connection);
                connection.Open();
                // Create DbDataReader to Data Worksheet
                DbDataReader dr = command.ExecuteReader();

                // SQL Server Connection String
                string sqlConnectionString = @"Data Source=GSSTOK2PC;Initial Catalog=ihaletakip;Integrated Security=True";

                // Bulk Copy to SQL Server 
                SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                bulkInsert.DestinationTableName = "ihalekayit";
                bulkInsert.WriteToServer(dr);

            }

            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog rs = new OpenFileDialog();
                rs.Filter = " Exel dosyaları |*.xlsx| Bütün Dosyalar|*.*";
                rs.Title = "1yada0.com";
                rs.ShowDialog();
                textBox1.Text = rs.FileName.ToString();
            }
        }
    }
    Wednesday, August 21, 2013 7:02 AM

All replies