Introduction
This trick is a complete solution to the programmers who want a solution to read CSV file using Open file dialog, store the raw data in a Data Grid View control and lastly insert the raw data in a SQL table using SQLbulkcopy.
Using the Code
First u need to create a Database with a table named Company for example. The table creation SQL script is provided below.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Company]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Company]
GO
CREATE TABLE [dbo].[Company] (
[CompanyName] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Volume] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
The sql table column names have to be similar to the column names that you have in the .CSV file .
You need to create a C#.Net Project and
place the following controls on a form:
1.Three Textbox
2.Three Buttons
3.One DataGridView
4.Lastly one OpenFileDialogControl
And paste the code and modify the above controls name as below
I am not going to indent the following code. ;P
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.SqlClient;
namespace FileReader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtServerConfiguration.Text.Trim()))
{
MessageBox.Show("Please supply Server Configuration");
return;
}
if (string.IsNullOrEmpty(txtTableName.Text.Trim()))
{
MessageBox.Show("Please supply Table Name");
return;
}
SqlBulkCopy sqlbulkCopy = new SqlBulkCopy(txtServerConfiguration.Text.Trim());
sqlbulkCopy.DestinationTableName = txtTableName.Text.Trim();
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
sqlbulkCopy.ColumnMappings.Add(dataGridView1.Columns[i].Name,
dataGridView1.Columns[i].Name);
}
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
sqlbulkCopy.WriteToServer(dt);
sqlbulkCopy.Close();
MessageBox.Show("Saved Successfully");
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
private void btnLoadFile_Click(object sender, EventArgs e)
{
try
{
DialogResult dResult = new DialogResult();
dResult = openFileDialog1.ShowDialog();
if (dResult == DialogResult.OK)
{
txtFilePath.Text = openFileDialog1.FileName;
}
else
{
return;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
private void btnLoadData_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtFilePath.Text.Trim()))
{
MessageBox.Show("Please supply file name");
return;
}
System.IO.StreamReader rdr = new System.IO.StreamReader(txtFilePath.Text.Trim());
string inputLine = "";
DataTable dt = new DataTable();
dt.Columns.Add("CompanyName");
dt.Columns.Add("Volume");
DataRow row;
while ((inputLine = rdr.ReadLine()) != null)
{
string[] arr;
arr = inputLine.Split(',');
row = dt.NewRow();
row["Companyname"] = arr[0];
row["Volume"] = arr[1];
dt.Rows.Add(row);
}
dt.Rows.RemoveAt(0);
dataGridView1.DataSource = dt;
rdr.Close();
}
catch (Exception)
{
throw;
}
}
}
}
Take a look on the code.
You need to do the follwing:
- Define a server configuration.
- Set a Table Name
That's it. Happy coding :-D