I'm trying to insert specific columns from excel into my database on sql server and here's the following code:
constr = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;""", FilePath);
Econ = new OleDbConnection(constr);
SqlConnection con = new SqlConnection("Data Source=(local);Database='SimDevice_Stocks';Integrated Security= yes;");
Query = string.Format("Select [ORDER_SUB_TYPE],[ORDER_DATE],[ORDER_STATUS],[SIM_NETWORK_TYPE],[STORE_ID],[ROOT_PRODUCT_ATM_TYPE],[STC_ERP_SIM_ITEM],[STC_SALECO_ITEM_CODE] FROM [{0}] where ORDER_SUB_TYPE='Change SIM' and ORDER_SUB_TYPE='New'and ORDER_STATUS = 'Complete' and ROOT_PRODUCT_ATM_TYPE='Prepaid'", "Online_Inventory_Open_Points_re$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(con);
//assigning Destination table name
objbulk.DestinationTableName = "Temp_DailyInventory";
//Mapping Table column
objbulk.ColumnMappings.Add("ORDER_SUB_TYPE", "Order_Sub_Type");
objbulk.ColumnMappings.Add("Order_Date", "Order_Date");
objbulk.ColumnMappings.Add("ORDER_STATUS", "Order_Status");
objbulk.ColumnMappings.Add("SIM_NETWORK_TYPE", "Sim_Network_Type");
objbulk.ColumnMappings.Add("STORE_ID", "Store_ID");
objbulk.ColumnMappings.Add("ROOT_PRODUCT_ATM_TYPE", "Root_Product_Atm_Type");
objbulk.ColumnMappings.Add("STC_ERP_SIM_ITEM", "Stc_Erp_Sim_Item");
objbulk.ColumnMappings.Add("STC_SALECO_ITEM_CODE", "Stc_Saleco_Item_Code");
con.Open();
objbulk.WriteToServer(Exceldt);
con.Close();
so i've got 2 type of data in my excel sheet column of the Order_Sub_Type it got Change sim and New so i need to enter those 2 records but i'm not able to insert them as you can see in the above query i can either choose one to enter "Change Sim" or "New".
Thanks in advance
Related
I have a datatable table which have multiple records. I want to Insert this datatable to ms-access table without using loop.
I want to insert multiple rows/records into ms-access database as whole. I don't want to insert records one by one.
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
var adap = new OleDbDataAdapter();
adap.SelectCommand = new OleDbCommand ("select RollNo, SName, FName, DOB, [Section] from students", conn);
var cb = new OleDbCommandBuilder(adap);
cb.GetInsertCommand();
cb.GetDeleteCommand();
cb.GetUpdateCommand();
conn.Open();
adap.Update(table);
}
Loading of data from excel sheet to datatable. code is below,
using (OleDbConnection connExcel = new OleDbConnection(DatabaseObjects.ConnectionStringExcel))
{
string queryExcel = "select * from [" + sheetName + "$]";
using (OleDbCommand commandExcel = new OleDbCommand(queryExcel, connExcel))
{
connExcel.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = commandExcel;
adapter.Fill(dtSheetData);
}
}
I am writing a component in C# which returns data from an EXCEl spreadsheet using Microsoft.ACE.OLEDB.12.0. The spreadsheet contains cells with formulas and references to other spreadsheets within that workbook. These cells return no data to the DataTable. See example below.
OleDbConnection OleDBconn = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=Yes;IMEX=1\"",InputFile));
OleDbCommand OleCommand = new OleDbCommand();
OleCommand.Connection = OleDBconn;
OleDBconn.Open();
dtXLS = OleDBconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
DataTable dt = new DataTable();
OleDbDataAdapter adp = new OleDbDataAdapter(OleCommand);
OleCommand.CommandText = string.Format(#"SELECT [Column] From [Sheet1$]");
adp.SelectCommand = OleCommand;
adp.Fill(dt);
Column contains cells with formulas and references to other worksheets in the workbook. So dt[0][Column] is null when it should have a value. The cell in the spreadsheet contains the below reference
=dd!B2
here is something that you can try in regards to filling and returning the DataTable
//call the method this way
var someDataTable = ExecuteDataSet("SELECT * FROM [Sheet1$]", InputFile);
public static DataTable ExecuteDataSet(string sql, string InputFile)
{
using (DataSet myDataset = new DataSet())
using (OleDbConnection OleDBconn = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=Yes;IMEX=1\"",InputFile));
using (OleDbCommand cmdSelect = new OleDbCommand(sql, OleDBconn))
{
try
{
OleDBconn.Open();
dtXLS = OleDBconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//if you need to return this change the method signature to out param for this
new OleDbDataAdapter(cmdSelect).Fill(myDataset);
}
catch (Exception ex)
{
//Show a message or log a message on ex.Message
}
return myDataset.Tables[0];
}
}
I am trying to import data from Excel sheet to local DB.(Excel 2010)
Coding is
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + filePath + #";Extended Properties=""Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0; ImportMixedTypes=Text""");
//Select Row no 1 to 65536
sql = "SELECT F1 FROM [Sheet1$F1:F65536] ";
OleDbCommand cmd = new OleDbCommand(sql, connection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
//Store into DataTable
DataSet ds = new DataSet();
ds.Tables.Add("Sheet1");
da.Fill(ds, "Sheet1");
//Name column anme
DataTable table = ds.Tables[tableName];
table.Columns[0].ColumnName = "CODE";
SqlConnection sqlCon = new SqlConnection(#"Data Source=(LocalDB)\v11.0; AttachDbFileName=|DataDirectory|\Report.mdf;Integrated Security=True");
sqlCon.Open();
//Store result into localDB
SqlBulkCopy bulk = new SqlBulkCopy(sqlCon);
bulk.DestinationTableName = tableName;
//Map column name "CODE"
foreach (DataColumn column in table.Columns)
bulk.ColumnMappings.Add(column.ColumnName.ToString(), column.ColumnName.ToString());
bulk.WriteToServer(table);
But when I try to select row No 65537, I get an error "The Microsoft Access database engine could not find the object 'Sheet1$F1:F65537'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$F1:F65537' is not a local object, check your network connection or contact the server administrator."
I also tried ""SELECT F1 FROM [Sheet1$F:F]".
But in table, only fetch 65536 data into table.
Do you have any idea to fetch after row no 65536?
According to OP:
I found solution. I have changed only query to "SELECT * FROM [Sheet1$] " and all data (300,000 data) has been stored.
Code:
constr = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
Econ = new OleDbConnection(constr); ExcelConn(FilePath);
Econ.Open();
Query = string.Format("Select [Emp ID],[Emp Name],[Log Date],[LogTime],[Type] FROM [{0}]", "One Month Report$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(conn);
//assigning Destination table name
objbulk.DestinationTableName = " Attendancetable";
//Mapping Table column
objbulk.ColumnMappings.Add("Emp ID", "Emp ID");
objbulk.ColumnMappings.Add("Emp Name", "Emp Name");
objbulk.ColumnMappings.Add("Log Date", "Log Date");
objbulk.ColumnMappings.Add("LogTime", "LogTime");
objbulk.ColumnMappings.Add("Type", "Type");
//inserting Datatable Records to DataBase
conn.Open();
objbulk.WriteToServer(Exceldt);
conn.Close();
In opened Excel sheet uploading is successful, but in closed Excel sheet file uploading process showing this error:
External table is not in expected formt.
I'm trying show a DataGrid in C# (for app WindowsMobile). I have a database ("pruebaDB.sdf") in DataConnections and one table ("tablaMercancia").
Also in DataSource I have "pruebaDBDataSet" and "tablaMercancia".
How I can show data table in a DataGrid?
I use a SmartDevice project (I can't to use DataGridView, only I use DataGrid).
I can show a new table (created for code) in a DataGrid, but I don't know to show an existing table in my database.
string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\pruebaDB.sdf;Persist Security Info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
//...............
//...Any idea?
//...............
connection.Close();
Any ideas please?
Thanks!!!
Please, Change Datagridview name as per below :
string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\pruebaDB.sdf;Persist Security Info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
//datagridview1 is name of datagridview in form:
datagridview1.DataSource=ds.Tables[0];
connection.Close();
Try this.
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
//SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(sql, connection);
DataSet ds=new DataSet();
da.Fill(ds);
designing page track the gridview or data grid
1st use namespace using System.Data,SqlClient;
sqlconnection con=new sqlconnection("string path");
con.open();
sqldataadapter da=new sqldataadapter("select * from emp",con);
dataset ds=new dataset();
da.fill(ds,"emp");
gridview1.datasource=ds;
gridview1.databind();