Posts

Showing posts with the label datatable

C# Use a stored procedure to fill a dataset/table

When you fill a DataSet/Table in C# you use a SQL Data Adapter. The code for this is geared towards a SQL SELECT statement. However I'm not a big fan of raw SQL in my code so I found a way to use a stored procedure instead. public static DataTable GetTable() { DataTable dt = null; try { using( SqlConnection connection = new SqlConnection( "your connection string" ) ) { using( SqlDataAdapter sda = new SqlDataAdapter() ) { using( SqlCommand command = new SqlCommand( "your stored procedure", connection ) ) { command.CommandType = CommandType.StoredProcedure; // this line needs to be added for every parameter the SP expects command.Parameters.AddWithValue( "parametere value", paramValue ); sda.SelectCommand = command; dt = new Da...

C# Finding all tables in access file

I recently found a need to access table names found in an access file dynamically. I found some code on the net to do this. But I discovered a bug, the code I was using would only read static tables. The files I was getting could contain linked and pass-through tables. So after some more searching I found the best way was to get all the tables (system, access) etc and then filter out only what I needed. So after some coding here is the solution. This is sitting in a function public static DataTable GetTableNames( string accessFile ) { OleDbConnection con = null; try { // create a connection string String connect = String.Format( "Provider=Microsoft.JET.OLEDB.4.0;data source={0}", accessFile ); // open the connection con = new OleDbConnection( connect ); con.Open(); // get all the tables in the file, system, etc DataTable dtAllTables = con.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object[] { null, null, null, null } ); DataTable filterTable...

Updated getting data from an Excel file in C#

Ok I posted how to get data from an excel file a bit ago. I have some update code that reads all files into a dataset, or reads from a specific file. You could easily modify this code to read from a specific sheet as well. public static DataSet GetAllSheetsFromExcelFile( string filename ) { DataSet ds; try { ds = new DataSet(); DataTable dtSheets = new DataTable(); // get a datatable with the worksheet name(s) OleDbConnection con = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0" ); con.Open(); // get a datatable of the sheetnames in this file DataTable dtNames = con.GetOleDbSchemaTable( System.Data.OleDb.OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" } ); // add each sheet as a new table to our dataset foreach( DataRow row in dtNames.Rows ) { DataTable dtTemp = new DataTable(); ...

Saving Changes to a DataTable bound to a DataGridView

Ok, So I'm working on app that binds a datatable to a data grid view. Normally you would just edit right in the grid and be done. However our table has too much data to edit right in the grid so instead users have to click a row on the grid to load a details panel below the grid. This works great except I noticed changes made to the last row weren't being saved. This is because the grids don't commit changes to their datasource unless a new row is clicked. So after a few hours of digging I found this bit of code worked very well. // commit all our changes to the datasource of our grid if( dataGridViewVariables.BindingContext[_tableEditVariables] != null ) {   dataGridViewVariables.BindingContext[_tableEditVariables].EndCurrentEdit(); }//end if Check that the object in this case a data table being used to bind exists. And if it does call the end current edit on that object. However if you're binding to a custom collection of objects I believe you need to...