select query at excel sheet - c#

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";

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();
}

External table is not in the expected format c#

I have seen more answers while googling it, but couldn't solved my problem.
This is the code I have written to insert excel sheet into sql server table.
string strConnection = ConfigurationManager.ConnectionStrings[0].ToString();
string path = #"D:/Projects/sample.xlsx";
string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [Name], [City], [Address], [Designation] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "temp1";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
While executing the line "excelConnection.Open();". I am getting an error "External table is not in the expected format."
And "sample.xlsx" is actually an ods format file. I changed its format as xlsx.
What would be wrong in this code. Please suggest me.
As per the conversation in the comments, you have to convert the ODS file into XLS or XLSX format not just renaming it. You can use Excel or Online tools to convert ODS files.

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].

Import data from excel to mysql using 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()

Categories

Resources