Import data from excel to mysql using c# - c#

I have Excel file shown bellow
I want to read 1st read only all school names & school address & insert them in SchoolInfo table of mySql database.
After that I want to read data for each school & insert it in StudentsInfo table which has foreign key associated with SchoolInfo table.
I am reading excel sheet something like this.
public static void Import(string fileName)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\"";
var output = new DataSet();
using (var conn = new OleDbConnection(strConn))
{
conn.Open();
var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
if (dt != null)
foreach (DataRow row in dt.Rows)
{
string sheet = row["TABLE_NAME"].ToString();
var cmd = new OleDbCommand("SELECT * FROM [+"+sheet+"+]", conn);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter xlAdapter = new OleDbDataAdapter(cmd);
xlAdapter.Fill(output,"School");
}
}
}
Now I am having data in datatable of dataset, Now how do I read desired data & insert it in my sql table.

Try the following steps:
Reading from Excel sheet
First you must create an OleDB connection to the Excel file.
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + path + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection xlConn = new OleDbConnection(connectionString);
xlConn.Open();
Here path refers to the location of your Excel spreadsheet. E.g. "D:\abc.xls"
Then you have to query your table. For this we must define names for the table and columns first. Click here to find out how.
Now create the command object.
OleDbCommand selectCmd = new OleDbCommand("SELECT * FROM [Sheet1$]", xlConn);
Now we have to store the ouput of the select command into a DataSet using a DataAdapter
OleDbDataAdapter xlAdapter = new OleDbDataAdapter();
objAdapter1.SelectCommand = selectCmd;
DataSet xlDataset = new DataSet();
xlAdapter.Fill(xlDataset, "XLData");
Save the data into variables
Now extract cell data into variables iteratively for the whole table by using
variable = xlDataset.Tables[0].Rows[row_value][column_value].ToString() ;
Write the data from the variables into the MySQL database
Now connect to the MySQL database using an ODBC connection
String mySqlConnectionString = "driver={MySQL ODBC 5.1 Driver};" +
"server=localhost;" + "database=;" + "user=;" + "password=;";
OdbcConnection mySqlConn = new OdbcConnection(mySqlConnectionString);
mySqlConn.Open();
Construct your INSERT statement using the variables in which data has been stored from the Excel sheet.
OdbcCommand mySqlInsert = new OdbcCommand(insertCommand, mySqlConn);
mySqlInsert.ExecuteScalar()

Related

How to insert excel dynamic columns names to sql using C#

I want to insert excel data to sql, but excel column names are changing frequently.
Below is the code I have written for excel bulk upload
string excelConnectionString = string.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES""", path);
//// Create Connection to Excel Workbook
{
string destablename1 = string.Empty;
destablename1 = "[admin.AttendanceData]";
DataTable Data = new DataTable();
using (OleDbConnection conn = new OleDbConnection(excelConnectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM [Attendance$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(Data);
conn.Close();
}
var dacObj = new DAC();
String strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strConnection))
{
bulkCopy.ColumnMappings.Add("Emp ID", "Citrix_ID");
bulkCopy.ColumnMappings.Add("Full Name", "Full_Name");
bulkCopy.ColumnMappings.Add("Band", "Band");
bulkCopy.ColumnMappings.Add("Cigna DOJ", "Cigna_DOJ");
bulkCopy.DestinationTableName = destablename1;
dacObj.OpenConnection();
bulkCopy.WriteToServer(Data);
dacObj.CloseConnection();
}
}n
how to add dynamic column names from excel to sql?
sample picture of excel sheet data
Here after Cigna DOJ columns will change for every month.

C# - How to insert data from SQL table to Excel template using Console Application

I have different SQL tables that I need to export to Excel file.
I created a blank Excel Template with cell formatting but no data, even column names.
Is there a way to insert data from SQL table with undefined numbers of columns to an existing Excel Template?
I found this code, but it's not working:
string fileName = #"C:\test.xls";
string connectionString = String.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName);
using (OleDbConnection cn = new OleDbConnection(connectionString))
{
cn.Open();
OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sheet1$] " +
"VALUES(#value1, #value2, #value3, #value4)", cn);
cmd1.Parameters.AddWithValue("#value1", "Key1");
cmd1.Parameters.AddWithValue("#value2", "Sample1");
cmd1.Parameters.AddWithValue("#value3", 1);
cmd1.Parameters.AddWithValue("#value4", 9);
cmd1.ExecuteNonQuery();
}

How to read and get data from excel .xlsx

I have excel file with 2 tables. I need to read this tables and get all the values from this tables. But all for what I have is:
OleDbConnection cnn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MigrateExelSql\Include\TestDb.xlsx; Extended Properties=Excel 12.0;");
OleDbCommand oconn = new OleDbCommand("select * from [Sheet1$]", cnn);
cnn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
And I don't uderstand what I need to write for get the all values from Username and Email tables. Here is the .xlsx table TestDb Please can somebody help me, because I'm googling the second day and I have no idea for what I must to do.
And when I try to get values by this method it return me an error:
var fileName = string.Format("{0}\\Include\\TestDb.xlsx", Directory.GetCurrentDirectory());
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "Username");
var data = ds.Tables["Username"].AsEnumerable();
foreach (var item in data)
{
Console.WriteLine(item);
}
Console.ReadKey();
One more Edit:
string con =
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MigrateExelSql\Include\TestDb.xlsx; Extended Properties=Excel 12.0;";
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
}
}
}
Console.ReadKey();
This will read only first column, but when I try to read dr[1] it will return error: Index was outside bound of the array.
Your xlsx file contains only one sheet and in that sheet there is only one column.
A sheet is treated by the OleDb driver like a datatable and each column in a sheet is considered a datacolumn.
You can't read anything apart one table (Sheet1$) and one column (dr[0]).
If you try to read dr[1] then you are referencing the second column and that column doesn't exist in Sheet1.
Just to test, try to add some values in the second column of the Excel file.
Now you can reference dr[1].

Query on two DataTable to find duplicate rows

I have an excel sheet which i am uploading to sqlserver table using sqlbulkcopy, i have applied an index on database table so that duplicate entries are ignored but i also want to display the duplicate entries which were ignored in a grid view.
protected void Button1_Click(object sender, EventArgs e)
{
string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
string strFileName = FileUpload1.PostedFile.FileName.ToString();
FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);
string excelConnectionString = String.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+strNewPath +"; Extended Properties=Excel 8.0;");
//string excelConnectionString = String.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\myFolder\\Book1.xls;" + "Extended Properties=Excel 8.0;");
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);
OleDbCommand command1 = new OleDbCommand("select count(*) from [Sheet1$]",connection);
//Sql Server Table DataTable
DataTable dt4=new DataTable();
SqlCommand cmd4 = new SqlCommand("select id,data from ExcelTable", con);
try
{
SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
da4.Fill(dt4);//sql table datatable
}
catch { }
//excelsheet datatable
DataTable oltlb = new DataTable();
OleDbCommand olcmd = new OleDbCommand("select id,data from [Sheet1$]", connection);
try
{
OleDbDataAdapter olda = new OleDbDataAdapter(olcmd);
olda.Fill(oltlb); //excel table datatable
}
catch { }
var matched = (from table1 in dt4.AsEnumerable()
join table2 in oltlb.AsEnumerable() on
table1.Field<int>("ID") equals table2.Field<int>("ID")
where table1.Field<string>("Data") == table2.Field<string>("Data")
select table1);
DataTable dthi5 = new DataTable();
dthi5 = matched.CopyToDataTable();
GridView1.DataSource = dthi5;
GridView1.DataBind();
GridView1.DataSource = matched.ToList();
ERROR:
Specified cast is not valid.
table1.Field("ID") equals table2.Field("ID")
It would appear that the ID field in one of your tables is not an int.
Rather than one or both of us guessing, try inspecting the data type of the first column in both tables (oltlb and dt4) to see which has the non-integer data type. It may in fact be both.
Here's the thing: even if the value of a string is a numeric literal, you can't cast a string to an int. You'd have to use Int32.Parse or Convert.ToInt32 to convert. So if one of your tables has a string type for the ID column, you would express it like this:
table1.Field<int>("ID") equals Convert.ToInt32(table2.Field<string>("ID"))
If both of the tables have a string ID field, you could just express it this way:
table1.Field<string>("ID") equals table2.Field<string>("ID")

select query at excel sheet

i have a problem with select query. My aim is selecting some columns in excel sheet and copy them in my sql table. i have six colums to copy . When i tried to copy orders changing because of the identity column.
Here is my code;
string path = Server.MapPath(Session["excel_sheet"].ToString());
//Create connection string to Excel work book
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd2 = new OleDbCommand("Select [column_1],[column_2],[column_3],[column_4],[column_5],[column_6] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd2.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(con);
//Give your Destination table name
sqlBulk.DestinationTableName = "MYtable";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
my table has columns like this identity_column,column_1,column_2,column_3,column_4,column_5,column_6.
After the copy for example in excel column_4 strings writes to sql column_3 it must write to column_4 . i'm wating for an answer
You can iterate through the columns and map them, this will map the column names in excel to the same name in the connection (ie Excel column_1 = sql column_1)
SqlBulkCopy sqlBulk = new SqlBulkCopy(con);
//Define column mappings
for (int i = 0; i < dReader.FieldCount; i++)
{
sqlBulk.ColumnMappings.Add(dReader.GetName(i), dReader.GetName(i));
}
You should use ColumnMappings property of SqlBulkCopy Class.
for ex:
sqlBulk.ColumnMappings.Add("column_1", "column_1");
sqlBulk.DestinationTableName = "MYtable";

Categories

Resources