datatable interview questions
Top datatable frequently asked interview questions
what is the best way to check if a Data Table has a null value in it ?
Most of the time in our scenario, one column will have all null values.
(This datatable is returned by a 3rd party application - we are trying to put a valiadation before our application processes the data-table)
Source: (StackOverflow)
We have two columns in a DataTable
, like so:
COL1 COL2
Abc 5
Def 8
Ghi 3
We're trying to sort this datatable
based on COL2
in decreasing order.
COL1 COL2
ghi 8
abc 4
def 3
jkl 1
We tried this:
ft.DefaultView.Sort = "occr desc";
ft = ft.DefaultView.ToTable(true);
but, without using a DataView
, we want to sort the DataTable
itself, not the DataView
.
Source: (StackOverflow)
How do create a DataTable in C#?
I did like this:
DataTable dt = new DataTable();
dt.clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
How do I see the structure of DataTable?
Now I want to add ravi for Name
and 500 for Marks
. How can I do this?
Source: (StackOverflow)
I currently use a DataTable to get results from a database which I can use in my code.
However, many example on the web show using a DataSet instead and accessing the table(s) through the collections method.
Is there any advantage, performance wise or otherwise, of using DataSets or DataTables as a storage method for SQL results?
Source: (StackOverflow)
How can I load a CSV file into a System.Data.DataTable, creating the datatable based on the CSV file?
Is there a class library for this or can I use ADO.net to connect to the file?
Source: (StackOverflow)
Currently, I'm using:
DataTable dt = CreateDataTableInSomeWay();
List<DataRow> list = new List<DataRow>();
foreach (DataRow dr in dt.Rows)
{
list.Add(dr);
}
Is there a better/magic way?
Source: (StackOverflow)
I have a dataset objds. objds contains a table named Table1. Table1 contains column named ProcessName. This ProcessName contains repated names.So i want to select only distinct names.Is this possible.
intUniqId[i] = (objds.Tables[0].Rows[i]["ProcessName"].ToString());
Source: (StackOverflow)
I have few methods that returns different Generic Lists.
Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this.
IF i have this:
List<Whatever> whatever = new List<Whatever>();
(This next code doesn't work of course, but i would like to have the possibility of:
DataTable dt = (DataTable) whatever;
Source: (StackOverflow)
DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods.
However, from what I've read so far, DataSet and DataTable don't actually have any unmanaged resources, so Dispose() doesn't actually do much.
Plus, I can't just use using(DataSet myDataSet...)
because DataSet has a collection of DataTables.
So, to be safe, I'd need to iterate through myDataSet.Tables, dispose of each of the DataTables, then dispose of the DataSet.
So, is it worth the hassle to call Dispose() on all of my DataSets and DataTables?
Addendum:
For those of you who think that DataSet should be disposed:
In general, the pattern for disposing is to use using
or try..finally
, because you want to guarantee that Dispose() will be called.
However, this gets ugly real fast for a collection. For example, what do you do if one of the calls to Dispose() thrown an exception? Do you swallow it (which is "bad") so that you can continue on to dispose the next element?
Or, do you suggest that I just call myDataSet.Dispose(), and forget about disposing the DataTables in myDataSet.Tables?
Source: (StackOverflow)
I'm trying to perform a LINQ query on a DataTable object and bizarrely I am finding that performing such queries on DataTables is not straightforward. For example:
var results = from myRow in myDataTable
where results.Field("RowNo") == 1
select results;
This is not allowed. How do I get something like this working?
I'm amazed that LINQ queries are not allowed on DataTables!
Source: (StackOverflow)
How can I monitor an SQL Server database for changes to a table without using triggers or modifying the structure of the database in any way? My preferred programming environment is .NET and C#.
I'd like to be able to support any SQL Server 2000 SP4 or newer. My application is a bolt-on data visualization for another company's product. Our customer base is in the thousands, so I don't want to have to put in requirements that we modify the third-party vendor's table at every installation.
By "changes to a table" I mean changes to table data, not changes to table structure.
Ultimately, I would like the change to trigger an event in my application, instead of having to check for changes at an interval.
The best course of action given my requirements (no triggers or schema modification, SQL Server 2000 and 2005) seems to be to use the BINARY_CHECKSUM function in T-SQL. The way I plan to implement is this:
Every X seconds run the following query:
SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM sample_table WITH (NOLOCK);
and compare that against the stored value. If the value has changed, go through the table row by row using the query
select row_id,BINARY_CHECKSUM(*) from sample_table WITH (NOLOCK);
and compare the returned checksums against stored values.
Source: (StackOverflow)
How do I check for the existence of a column in a datarow?
I'm building datatables to organize some data that I've already pulled back from the database. Depending on the type of data in each row, I need to create a datatable with different columns. Then, later on, I want to check and see if the datatable I am looking at has a certain column.
I know I can catch the exception and handle it that way, but I'm curious if there is a property or method on the datarow object that will do this for me?
Here's how I can do it by catching the exception:
public static String CheckEmptyDataRowItem(DataRow row, String rowName, String nullValue)
{
try
{
return row[rowName].ToString();
}
catch (System.ArgumentException)
{
return nullValue;
}
}
Source: (StackOverflow)
I know we can easily do this will simple loop, but i want to persue this LINQ/Predicate?
string[] columnNames = dt.Columns.?
or
string[] columnNames = from DataColumn dc in dt.Columns select dc.name;
Source: (StackOverflow)
I'm trying to perform a simple LINQ query on the Columns property of a DataTable:
from c in myDataTable.Columns.AsQueryable()
select c.ColumnName
However, what I get is this:
Could not find an implementation of the query pattern for source type 'System.Linq.IQueryable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'c'.
How can I get the DataColumnCollection to play nice with LINQ?
Source: (StackOverflow)
How to change Datatable columns oder in c#.
example:
am created sql table type order is Qty,Unit,Id but in program DataTable order is Id,Qty,Unit. In code Behind am directly pass DataTable to sql table type so the table order is different.
DataTable columns are :`Id,Qty,Unit.` i want this to be :`Qty,Unit,Id`
please help
Source: (StackOverflow)