I try to read excel file without open and fill it to dataset (for futher using), then I will save dataset to access database.
Here is my code
DataSet ds = new DataSet();
//Read data from excel file and store to dataset
var ExcelfileName = #"C:\Users\gsa81hc\Desktop\COEMInternalErrataTracking_withCQnumber.xlsx";
var ExcelcnnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelfileName + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
var Excelcnn = new OleDbConnection(ExcelcnnString);
Excelcnn.Open();
var cmd = Excelcnn.CreateCommand();
cmd.CommandText = "SELECT * FROM [GEN4 Proejects$] ";
var adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
//Save dataset to access database
string AccesscnnString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tool_Development\CSharp\Dot2Wpf\Project_Database.accdb";
var Accesscnn = new OleDbConnection(AccesscnnString);
Accesscnn.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand("SELECT * FROM Table", Accesscnn);
var objCommandBuilder = new OleDbCommandBuilder(da);
objCommandBuilder.QuotePrefix = "[";
objCommandBuilder.QuoteSuffix = "]";
da.Update(ds, "Table");
When I debug, ds has data but I don't know why when da.update can't save ds to access file
Related
I am trying to replace data in a dataset so i can add it back to a .DBF file. currently i can gather the new dataset but i don't know how to find and replace certain data. For instance, I want to replace 'ABC_123_TEST' by 'DEF_456_TEST' in the first column.
try
{
string coonect = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\; Extended Properties = dBase IV; User ID =; Password =";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = coonect;
con.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM variable WHERE NAME LIKE #VAL1", con);
cmd.SelectCommand.Parameters.AddWithValue("#VAL1", "%" + textBox5.Text + "%");
DataSet ds = new DataSet();
cmd.Fill(ds);
DataSet newData = ds.Copy(); //What should i do with this dataset
con.Close();
dataGridView1.DataSource = ds.Tables[0];
}
catch (OleDbException exp)
{
MessageBox.Show("Error: " + exp.Message);
}
I need to insert more than 1000 records into SQL Server. But using my code I am able to insert only 1000 records. Please help me.
using (SqlBulkCopy s = new SqlBulkCopy(dbConnection,SqlBulkCopyOptions.UseInternalTransaction, null))
{
s.DestinationTableName = TableName;
s.BatchSize = 10000;
s.BulkCopyTimeout = 1800;
foreach (var column in dt.Columns)
{
s.ColumnMappings.Add(column.ToString(), column.ToString());
}
s.WriteToServer(dt);
}
Below is the real time working code which i used in my project to insert the bulk data from excel to the SQL server
C# code:
public static DataSet Bindgrid_StoreInSQL(string path)
{
string strFileType = Path.GetExtension(path).ToLower();
string connString = "";
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable Exceldt = ds.Tables[0];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(OneStopMethods_Common.constring_Property);
//assigning Destination table name
objbulk.DestinationTableName = "Tern_boq";
objbulk.ColumnMappings.Add("ID", "ID");
objbulk.ColumnMappings.Add("Bill_No", "Bill_No");
objbulk.ColumnMappings.Add("Page_No", "Page_No");
objbulk.ColumnMappings.Add("ItemNo", "ItemNo");
objbulk.ColumnMappings.Add("Description", "Description");
objbulk.ColumnMappings.Add("BOQ_Qty", "BOQ_Qty");
objbulk.ColumnMappings.Add("UNIT", "UNIT");
objbulk.ColumnMappings.Add("Category1", "Category1");
objbulk.ColumnMappings.Add("Category2", "Category2");
objbulk.ColumnMappings.Add("Category3", "Category3");
objbulk.ColumnMappings.Add("Estimated_UnitRate", "Estimated_UnitRate");
objbulk.ColumnMappings.Add("Estimated_Amount", "Estimated_Amount");
//inserting Datatable Records to DataBase
conn.Open();
objbulk.WriteToServer(Exceldt);
SqlDatabase obj = new SqlDatabase(OneStopMethods_Common.constring_Property);
string selquery = " select * from Tern_boq";
return obj.ExecuteDataSet(CommandType.Text, selquery);
}
Its works fine,Hope this can give you some idea,Please let me know your your thoughts or suggestions
My excel simulation needs to be imported into C#, after which the table needs to be able to be refreshed. The simulation revolves around randomly generated numbers. The random numbers are the only columns that change, since I'm doing that manually. The surrounding columns should update with the random numbers. I have tried various things but no luck so far.
Also, as the code is now, the
adp.Update(excelDataSet);
command invokes the error "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." The table is only loaded into the gridview at all when it is commented out.
Here is my code atm. Thanks in advance.
string fileName = #"C:\simulation.xlsx";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;READONLY=FALSE\"";
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
con.Open();
OleDbCommand selectCommand = new OleDbCommand("select * from [SHEET1$]", con);
OleDbDataAdapter adp = new System.Data.OleDb.OleDbDataAdapter();
adp.SelectCommand = selectCommand;
DataSet excelDataSet = new DataSet();
adp.Fill(excelDataSet);
for (int i = 0; i < 29; i++)
{
excelDataSet.Tables[0].Rows[i][1] = Math.Round(r.NextDouble(), 2);
excelDataSet.Tables[0].Rows[i][6] = Math.Round(r.NextDouble(), 2);
excelDataSet.Tables[0].Rows[i][8] = Math.Round(r.NextDouble(), 2);
}
adp.Update(excelDataSet);
gridview.DataSource = excelDataSet.Tables[0];
con.Close();
Add a line that build a OleDbCommandBuilder
....
OleDbDataAdapter adp = new System.Data.OleDb.OleDbDataAdapter();
adp.SelectCommand = selectCommand;
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp);
adp.UpdateCommand = cb.GetUpdateCommand();
....
This will create the UpdateCommand in the OleDbDataAdapter for you, (can be used also for the InsertCommand and DeleteCommand)
I'm trying to open .dbf via c# wpf and load it into a ListView, but I have no luck.
In my ViewModel:
public void DBF()
{
var databasePath = #"C:\Users\jesson\Desktop\FLCOLU_Building_Outline_Hints_308EL_section3_2180ER_QC.dbf";
var connectionString = string.Format("DSN=dBase Files", databasePath);
OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();
var _command = connection.CreateCommand();
var query = string.Format(#"SELECT * FROM C:\Users\jesson\Desktop\FLCOLU_Building_Outline_Hints_308EL_section3_2180ER_QC.dbf");
string commandText = query;
var _dataAdapter = new OdbcDataAdapter(commandText, connection);
DataSet _dataSet = new DataSet();
DataTable _dataTable = new DataTable();
_dataSet.Reset();
_dataAdapter.Fill(_dataSet);
_dataTable = _dataSet.Tables[0];
var rows = _dataTable.Rows;
string userName = rows[0].ItemArray[1] as string;
string password = rows[0].ItemArray[2] as string;
UserDataVar = new InputDataSingle
{
UserName = userName,
Password = password
};
connection.Close();
}
Did I do something wrong? Any other ideas?
here you go. try below one.
string filename= //yourfilePath;
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ filename +";Extended Properties=dBASE IV;User ID=Admin;Password=;";
DataTable dt = new DataTable();
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from " + filename ;
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet(); ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
dt =ds.Tables[0]
}
here how to pass data to listview or dataGrid.;
mylistview.ItemsSource = dt.DefaultView;
Condition
you have to create View to your ListView;
I have an excel file with one worksheet. I'm using MicroSoft.Office.Interop.Excel to read this file and then perform further execution.
Here is my code:
connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strNewPath + ";Extended Properties=Excel 8.0;";
conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
System.Data.DataTable dt = null;
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
But, worksheet is not in the data table object.
Where you have mentioned the table(WorkSheet) name ??
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
//Get the First Sheet Name
string firstSheetName = schemaTable.Rows[0][2].ToString();
//Query String
string sql = string.Format("SELECT * FROM [{0}],firstSheetName);
Refer here MSDN
In case if you want to play around refer Reading Excel files from C#
OleDbConnection oconn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + "; Extended Properties=Excel 12.0;");
//After connecting to the Excel sheet here we are selecting the data
//using select statement from the Excel sheet
oconn.Open();
DataTable dbSchema = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
string tbstrat = "M1000";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = oconn;
cmd.CommandText = "select * from [" + firstSheetName + "B8:" + tbstrat + "]";
OleDbDataAdapter adap = new OleDbDataAdapter();
DataTable dt = new DataTable();
adap.SelectCommand = cmd;
adap.Fill(dt);
oconn.Close();
Even you can try this
var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "NameHere");
DataTable data = ds.Tables["NameHere"];