SSIS Script Task: Select from datatable1 into datatable2 - c#

I'm using SQL 2014, Visual Studio 2013. I have the following code that's part of a larger script task:
//Load DataTable1 with Source Excel Sheet Data
OleDbCommand oconn = new OleDbCommand("select * from [" + sheetname + "]", cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
What I would like to do next is select a targeted number of columns from datatable1, and fill another datatable. Something like this:
//Load another DataTable with Data from datatable1
OleDbCommand oconn2 = new OleDbCommand("select [column1], [column2] from datatable1", cnn);
OleDbDataAdapter adp2 = new OleDbDataAdapter(oconn2);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
Is this possible? The reason behind this is complicated, but I don't know another way around it. My source files are Excel. So the data needs to be brought into a datatable. Then the column names need to modified. Then a table can be made with the modified column names. But first, the data needs to be brought into a datatable as is, as I'm not sure I want to modify the source files.
Thank you.

Define the second DataTable using the data types and new names for the columns that will be added to it. Then you can populate this object by using the ordinal position of the columns in the initial DataTable with the Add method as below.
DataTable dt2 = new DataTable();
dt2.Columns.Add("NewColumnName1", typeof(int));
dt2.Columns.Add("NewColumnName2", typeof(string));
//in this example dt is the original DataTable
foreach (DataRow dr in dt.Rows)
{
//add only necessary columns by their
//ordinal position in source DataTable
dt2.Rows.Add(dr[1], dr[0]);
}

Related

Adding Row to datatable in dataset

I am trying to copy a row from one table to another in same dataset.
code I am using
dsFrom.Tables["asd2"].Rows.Add(dsFrom.Tables["asd"].Rows[0].ItemArray);
I am getting an NullRefferenceException. I've determined that Rows are null, even though there is data in both tables. Can anyone explain why is this happening? Or maybe there is another solution for my problem.
Thanks
EDIT
This is how I am loading data in them
string query = #"select * from table1;
SqlDataAdapter da = new SqlDataAdapter(query, conn);
DataSet dsFrom = new DataSet();
da.Fill(dsFrom, "asd");
da.Fill(dsFrom, "asd2");
May be you should try to copy rows by this way:
foreach (var row in dsFrom.Tables["asd"].Rows)
{
dsFrom.Tables["asd2"].ImportRow(row);
}

SqlDataAdapter.FillSchema() method doesn't work

I do the following:
SqlConnection conn = new SqlConnection("... my connection string ...");
dataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand("select * from items", conn);
dataAdapter.SelectCommand = selectCommand;
DataTable schemaTable = new DataTable();
dataAdapter.FillSchema(schemaTable, SchemaType.Source);
However, there are no rows in the schemaTable. dataAdapter.Fill() work.
I think you misunderstand what FillSchema does.
It takes the DataTable passed in (with no row and no column) and builds the schema adding the columns with infos about size, type etc... corresponding to the table that will be returned by the SelectCommand.
It doesn't fill the passed in table with records.
Infact, if you look at the Columns count after the call to FillSchema you will find that your schemaTable has been 'built' with columns matching the items table.
So, what is the use for a FillSchema call considering that a Fill call, equally fills the table with its columns names and types? Well, FillSchema is used to prepare the table passed in for the following Fill with some properties that otherwise are not loaded by the Fill call. For example, suppose that your items table has an AutoIncrement column. This property is not available on the the matching DataColumn after the call to Fill. But, if you pass to Fill, the table prepared by FillSchema, that property is available.
If you want just the infos about the columns of your table you need a different approach
using(SqlConnection con = new SqlConnection(.......)
{
con.Open();
DataTable schema = con.GetSchema("Columns", new string[] {null, null, "items"});
foreach(DataRow row in schema.Rows)
Console.WriteLine("TABLE:" + row.Field<string>("TABLE_NAME") +
" COLUMN:" + row.Field<string>("COLUMN_NAME"));
}
or in a more standard way
using(SqlConnection con = new SqlConnection(.......)
{
con.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(#"SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='items'
AND TABLE_CATALOG = 'yourDBNameHere'", con);
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
foreach(DataRow row in dt.Rows)
..... catalog, name, ordinal_position, column_default etc....
}
The FillSchema method retrieves the schema from the database using the SelectCommand. A FillSchema creates a DataTable with the PrimaryKey and Constraints properties, adds the columns to the DataColumnCollection and configures the necessary DataColumn properties. This method doesn't fill the datatable with rows, it only applies the schema. use the Fillmethod to do this.

How to create columns with the names from a sqlite table in a dataGridView

I'm trying to search for a code entered by the user in a sqlite table if it's found add the row to a dataGridView. the problem is that the code I'm using clears the datagrid every time I add a new row.
Here is the code I'm using
DataTable dt = new DataTable();
String insSQL = "select * from Produtos where codigo =" + txtCodigo.Text;
String strConn = #"Data Source=C:\caixa.sqlite";
SQLiteConnection conn = new SQLiteConnection(strConn);
SQLiteDataAdapter da = new SQLiteDataAdapter(insSQL, strConn);
da.Fill(dt);
dataGridViewProdutos.DataSource = dt.DefaultView;
I tried to change the dataGridViewProdutos.datasource to dataGridViewProdutos.Rows.add(dt); them it adds a row but always empty.
I fixed it removing the DataTable dt = new DataTable(); from the loop. I also changed the dt.DefeulView to only dt as sugested by N4TKD and Im seeing no diference so Im using it.
Also thanks for the links it was very helpfull since I was struggling to make my colums fill the dataGridView space.

Copy Specific Dataset Columns to Datatable

i have a DataSet which reads xml data and it has almost 20 columns,
i need 5 columns from the DataSet .
i have tried at my level but not able to get the DataTable with the specific columns i need.
some codes which i tried are :
DataTable dt = new DataTable(); //dt is blank DataTable
DataSet ds = new DataSet(); //ds is existing DataSet which has single table in it
dt.Columns[0]=dst4.Tables[0].Columns[0];
dt.Columns.Add(dst4.Tables[0].Columns[0]);
ds.Tables.Add(dt)
dst4.Tables.Columns.Add(dt);
You could clone the table which creates an empty table with the same columns, then you can remove the redundant columns.Here is an example with named columns, you could also use the indices:
DataTable dt = ds.Tables[0].Clone();
var colToTake = new[] { "Col1", "Col3", "Col7", "Col15", "Col19" };
var colsToRemove = dt.Columns.Cast<DataColumn>()
.Where(c => !colToTake.Contains(c.ColumnName));
foreach (DataColumn colToRemove in colsToRemove)
dt.Columns.Remove(colToRemove);
say you have to copy columns "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5" to new DataTable
DataTable source = dst4.Tables[0];
DataTable dt = source.DefaultView.ToTable(false, "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5");

How to Get a Column number in Excel by its Column name

I created an Excel application using C# with MVVM model. In that excel file I created some columns as template columns. For example I am having some columns like Unit, Cost and Quantity. Now I want to find the exact column number of "Quantity" .
How can I get that particular column(Quantity) number?
Please can any one tell me some method to achieve this?
Execute the Select statement and fill the dataset. Now traverse through the column in the table and find the index of column 'Quantity".
Sample code to load the excel file in Dataset:
public static DataSet exceldata(string filelocation)
{
DataSet ds = new DataSet();
OleDbCommand excelCommand = new OleDbCommand(); OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter();
string excelConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filelocation + "; Extended Properties =Excel 8.0;Hdr=Yes";
OleDbConnection excelConn = new OleDbConnection(excelConnStr);
excelConn.Open();
DataTable dtPatterns = new DataTable(); excelCommand = new OleDbCommand("SELECT `PATTERN` as PATTERN, `PLAN` as PLAN FROM [PATTERNS$] where 1=0", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(dtPatterns);
"dtPatterns.TableName = Patterns";
ds.Tables.Add(dtPatterns);
return ds;
}
By using this method, you have loaded the table schema in dataset. Now you can store the column name and index in any of the collection variable and get the column index using the key column name in that collection.

Categories

Resources