C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

Getting Coloum name from excel sheet and creating database table Part 2 through c#


Posted Date: 09 Dec 2008    Resource Type: Code Snippets    Category: SQL
Author: Abhinav MisraMember Level: Silver    
Rating: 1 out of 5Points: 15




Hi all

In my previous part related to this issue i only show how to insert data from excel sheet to sql server

for reference that please see

http://dotnetspider.com/resources/22915-Code-Geting-column-name-Excel-sheet-create.aspx

In this part i have done the whole work done;Let see how it works

1.my code read the excel sheet to get its schema
2.then create SQL database table (name given by user)in database
3.then insert coloum name in table present in excel sheet
4.then inserting the coloum value of excel sheet in database table
using sqlbulkcopy.

Note:- u must first create database and your excel sheet should has it's coloum name in first row.

//VARIOUS OBJECTS AND CONNECTION STRING FOR EXCEL AND SQL

string Excel = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mca23.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
string SQL = "Data Source=.; Initial Catalog=hr;uid=sa;pwd=;Integrated Security=True";
SqlCommand cmd1;
SqlCommand cmdDatabase;
SqlConnection contoSql ;
OleDbConnection contoExcel;
OleDbCommand cmd;

//Extended Properties=""Excel 8.0;
THIS PART TELL OLEDB PROVIDER THAT IT IS GOING TO READ EXCEL SHEET
//HDR=YES;
THIS TELL DATA IS FROM THE FIRST ROW.


//CODE FOR GETTING SCHEMA
          
contoExcel = new OleDbConnection(Excel);
contoExcel.Open();
cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", contoExcel);
OleDbDataReader rdr = cmd.ExecuteReader();
DataTable schema = rdr.GetSchemaTable();
//[Sheet1$]
BY THIS SHEET1 IS TREATED LIKE DATABASE TABLE


//CREATING DATABASE TABLE

contoSql = new SqlConnection(SQL);
cmdDatabase = new SqlCommand("Create table "+txtdbname.Text+" ("+"id varchar(100));", contoSql);
contoSql.Open();
try
{
cmdDatabase.ExecuteNonQuery();
}

catch (Exception error)
{
MessageBox.Show("Table already exist,Please enter another name");
txtdbname.Focus();
txtdbname.Text = "";

}
contoSql.Close();

// I CREATED TABLE WITH ONE DEFAULT COLOUM BECAUSE IT NOT LETTING ME TO CREATING EMPTY TABLE



//CODE FOR INSERTING COLOUM PRESENT IN SHEET TO DATABASE TABLE
          
foreach (DataRow row in schema.Rows)
{
int i = 0;
foreach (DataColumn col in schema.Columns)
{ //
if (i == 0)
{
name = Convert.ToString(row[0]);
contoSql.Open();
cmd1 = new SqlCommand("ALTER TABLE "+dbname.TEXT+" ADD "+name+" varchar(1000);", contoSql);
cmd1.ExecuteNonQuery();
contoSql.Close();
}
i = i + 1;
}
}


//DROPING COLOUM ID BECAUSE IT IS NOT PRESENT IN SHEET OF EXCEL

cmd1.CommandText = "ALTER TABLE "+dbname+" DROP COLUMN id;";
cmd1.Connection = contoSql;
contoSql.Open();
cmd1.ExecuteNonQuery();


//INSERTING DATA FROM SHEET TO DATABASE.
  
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(contoSql))
{
bulkCopy.DestinationTableName = txtdbname.Text;
bulkCopy.WriteToServer(rdr);
contoSql.Close();
}
MessageBox.Show("Data inserted");



FOR ANY SUGGESTION PLEASE RESPONSE ME
REGARD Abhinav Misra



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
EXCEL  .  ADO.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Bulk Insert
Previous Resource: Searching data between two dates by ignoring time values
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use