How to make this table query more efficient - sql

I've added a dataset , and a table adapter to my C# project.(The dataset and tableadapter was added via DataSources box)
In this query , I get table data , then filter , then I will iterate and fill a listbox.
listBox1.Items.Clear();
ETPDataset.t_USR_UsersDataTable tbl = (new ETPDataset.t_USR_UsersDataTable());
ETPDatasetTableAdapters.t_USR_UsersTableAdapter tblAdap = new TestIntellisenseSql.ETPDatasetTableAdapters.t_USR_UsersTableAdapter();
tblAdap.Connection.ConnectionString = scon.ConnectionString;
tblAdap.Fill(tbl);
ETPDataset.t_USR_UsersRow[] rows2;
rows2 = (ETPDataset.t_USR_UsersRow[])tbl.Select("USR_RECORDID > 60");
foreach (ETPDataset.t_USR_UsersRow drow in rows2)
{
listBox1.Items.Add(drow.USR_UserID);
}
This is my problem
tblAdap.Fill(tbl);
because this line of code will fetch ALL data(problem for big tables) , how can i fix it ?
-Thanks

Create a new method on your adapter to use a where clause to filter the data at the database.

Related

Using Dataset and table adapters for Service Based Database in Visual Studio 2017 and VB.net

I am trying to create a Windows form with a self contained DB. I added a Service Based DataBase. I then created a Dataset. My table is simple:
Example:
columns are: Model (PK), Part1, Part2, Part3............
I want a combobox on my for to be able to select the model, and another combobox to select the desired part then return the number. You pick Model 740, and Part1 and the return is part1 part number.
The issue is that I need the select Statement in the tableadapter query builder to show something like this.
SELECT *
FROM Table
WHERE Model = ? (Paramter from ComboBox1) AND parameter (Part1 From Combobox2 Will be a Column Name)
I can't figure out how or if it is even possible to have a parameter in after the select.
Table is built like this:
[enter image description here][1]
[1]: https://i.stack.imgur.com/bf4kv.png
I got it figured out
'TODO: This line of code loads data into the 'CSDataset.CryoSeries' table. You can move, or remove it, as needed.
Me.CryoSeriesTableAdapter.Fill(Me.CSDataset.CryoSeries)
'Pulling Data From Table Adapter
Dim db As New DataTables.CSDatasetTableAdapters.CryoSeriesTableAdapter
Dim result = db.GetData
Dim Model As String = CBmodel.Text.Trim
Dim Part As String = CBPart.Text.Trim
TBresult.Text = result.Rows(0).Item(Part)

Delete all position in DbContext from BindingSource.List using Entity (EF6)

I have DbContext with table named:base.books
and BindingSource from LINQ query with records which I want to delete using RemoveRange.
Dim query = From books
In base.books
Where //some conditions
Select books.id, books.name,
Dim BooksSource As New BindingSource
BooksSource.DataSource = query.ToList
DataGridView_Books.DataSource = BooksSource.DataSource
After some changes in DataGridView_Books I want delete all positions from DataGridView_Books in base.books.
I imagine it that way:
listA=BooksSource.DataSource.List
''listB=convert(listA)
base.books.RemoveRange(listB)
base.books.SaveChanges()
but I do not know how to replace convert (I mean: 'System.Collections.IList' to 'System.Collections.IEnumerable')
Thanks.

SSIS update a column with a result of an sql select

I was wondering if there's a way of updating a column inside a dataflow task by running a select on every row?
Here's the situation :
Let's take this as our start position. I collect info from 2 files, then I merge them, and I add a column with the derived column tool. Is there a way of populating this column by performing a select on every row using the values of the the row?
Ex :
SELECT Count(*) AS cnt
FROM TABLE T
WHERE T.COLUMN1 = ROW.COLUMN3
AND T.COLUMN2 = ROW.COLUMN5
I don't know if I'm just not phrasing my need properly but I couldn't get any results
Thank you
You should be able to do this with a Lookup Transformation.
EDIT based on comment:
If you don't want to use a lookup due to the size of the table, you can do exactly what you want with a Script Component. You can create and execute your SQL Command for each row of the dataflow just like you would in any .net application.
I was able to do it with a Script Component
1- I've removed the Derived Column
2- I've created a string variable where I stored the query whith a wildcard string to replace every value that I need to get from the row.
3- I've passed this variable allong with one containing the connecection string info to the Script Component
4- I've added a new column to the Output Columns of the Script Component
5- Added using System.Data.OleDb;
6- Created 2 variables :
string jourFerieQuery;
string dbcsoledbschema;
7- Updated the PostExecute() to put the values of my SSIS variables into the script variables :
public override void PostExecute()
{
base.PostExecute();
jourFerieQuery = Variables.jourFerieQuery;
dbcsoledbschema = Variables.dbcsoledbschema;
}
8- Added a method :
int GetData(string cs, string query)
{
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
return (int)dt.Rows[0][0];
}
9- Updated the Input0_ProcessInputRow(Input0Buffer Row) :
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string query = jourFerieQuery.Replace("[1]", Row.CODDEVI).Replace("[2]", Row.DATCRBEZEROCONGE.ToString());
Row.Keep = GetData(dbcsoledbschema, query);
}
My query returns a count that's why the method I've added returns an int
You can do that through a script component (transformation).
Add an output of Ct.
I use System.Data.OleDB as it matches the SSIS package connection string.

Selecting column names and data types of columns as a table

I want to select column names of Table1 as rows of column1 of Table2 and data types of Table1 columns as rows of column2 of Table2.
So if Table1 is like this.
Name Age Graduation Date
John 21 11.11.2015
Hillary 23 7.09.2015
I want to get this table by query.
Name String
Age Long
Graduation Date Date
What should be my SQL query?
(I'm gonna use this in an Windows form application which uses OLEDB connection to get data from an mdb file.)
I'm gonna use this in an Windows form application which uses OLEDB
connection to get data from an mdb file
You can get the informations you need via con.GetOleDbSchemaTable:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
DataTable tableColumns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "Table1", null }));
foreach (DataRow row in tableColumns.Rows)
{
var columnNameColumn = row["COLUMN_NAME"];
var dateTypeColumn = row["DATA_TYPE"];
var ordinalPositionColumn = row["ORDINAL_POSITION"];
...
}
}
You will find column-names and types in this DataTable. Ref.

how to get data from a dynamic dataColumn in datatable mvc

I have a dataTable in which some columns are added dynamically. Now i want to get a dynamic dataColumn data from the dataTable.
List<bool> EngColumnData = new List<bool>(ResultsGrid.Rows.Count);
foreach(DataRow row in ResultsGrid.Rows)
{
EngColumnData.Add((bool)row["EnglishAchieved"]);
}
The above code works if the column is actually present in the dataTable. I am not sure about the dynamic Column.
Please help me with this.Thanks in advance.
First you can get list of columns names have in dynamic data DataTabel , like below ,
foreach(DataColumn col in table.Columns) {
List.Add(col.ColumnName);
}
this will give you list of columns
Then after get data coresponding to that data coloumns like below ,
foreach(DataRow row in table.Rows) {
Console.WriteLine(row[List.First()]);
}
You can use this way to solve your problem.