SQLite Inner Join Issue - c#

I have two tables with some odd join behavior. Here is a brief schema with data:
CREATE TABLE object(vnum INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE object_affect(vnum INTEGER, apply_id INTEGER, modifier INTEGER);
object
vnum name
10404 Test Item
object_affect
vnum apply_id modifier
10404 1 4
10404 5 2
10404 12 6
If I run the below query I should have 3 rows returned however it only returns 1 row.
select * from object o
inner join object_affect oa on oa.vnum = o.vnum
where o.vnum = 10404
If I change the query to this (change * to oa.vnum).. it return 3 rows:
select oa.vnum from object o
inner join object_affect oa on oa.vnum = o.vnum
where o.vnum = 10404
Also, if I remove the "PRIMARY KEY" flag in the create table statement from the vnum field on object table the first query that only returned one 1 returns all 3 joined rows correctly.
What am I missing that causes the first query to not return 3 rows of joined data?

I ran into this again 3.5 years later. I wanted to share the solution. The idea in this case is that the DataTable is used to display the results of a query on some kind of control, a DataGrid, etc. As a result, it doesn't need the constraints the DataTable tries to infer from the database about unique keys, etc. There's no good way to disable the constraints on the DataTable that I've found by itself but you can couple it with a DataSet and do just that. This snippet (assuming the SqliteCommand is already setup) works:
using (var dr = cmd.ExecuteReader())
{
var dt = new DataTable();
using (var ds = new DataSet() { EnforceConstraints = false })
{
ds.Tables.Add(dt);
dt.BeginLoadData();
dt.Load(dr);
dt.EndLoadData();
ds.Tables.Remove(dt);
}
}
Basically we temporarly use the DataSet and then discard it (it is important to remove it from the DataSet so the DataSet can Dispose and be garbage collected). Then you can keep the DataTable and persist it to bind it to a control.

Related

In Data table first column displaying with tablename.columnname in c#

When i am running query in data base and fill data table with load method first column name coming with table name.column name(Employee.Name,sal,location) rest of the column name coming only column names present in the database.Why table name appending first column name while displaying in data table structure please help me in this regards.
DataTable table = new DataTable();
try
{
using (IDbCommand DbCommand = dbConnection.CreateCommand())
{
DbCommand.CommandText = query;
IDataReader reader = DbCommand.ExecuteReader();
table.Load(reader);
}
}
Sample query Select Employee.Numnber, Employee.salary, Employee.city,dept.deptId from Employee inner join dept where deptiId=EmployeeDeptId
The actual query is :
Select
AP_LINE.LINE_NO, AP_LINE.PRODUCT, AP_LINE.QTY, AP_LINE.REQUESTED_DATE,
AP_LINE.LIST_PRICE, AP_LINE.CONS_NET_MULT,AP_LINE.NETADDERS,
AP_LINE.CONS_NET_PRICE, AP_LINE.Details,TDP_JSP.ID, TDP_JSP.LINE_NO,
TDP_JSP_AppInfo.Id
From AP_LINE, CONFIG, TDP_JSP
inner join TDP_JSP_AppInfo on TDP_JSP.ID= TDP_JSP_AppInfo.ID
where AP_LINE.LINE_NO = TDP_JSP.LINE_NO ;
Out put in data table as coming below
Ap_Line.Line_No,Product,QTY,Requested Date...
There are two LINE_NO columns in the query, AP_LINE.LINE_NO and TDP_JSP.LINE_NO. Columns must have unique names, so DataTable.Load used the two-part name wherever needed. It that wasn't possible it would start appending indexes or even generate names like Column0, Column1 etc.
In this case though, one of those columns isn't needed. The two tables are join those columns. WHERE AP_LINE.LINE_NO = TDP_JSP.LINE_NO is the old-style, frowned-upon join syntax. Those two columns will always have the same data so one of them can be removed.

How do I use LINQ to update a datatable with a SqlDataReader?

I am trying to merge data from two separate queries using C#. The data is located on separate servers or I would just combine the queries. I want to update the data in one of the columns of the first data set with the data in one of the columns of the second data set, joining on a different column.
Here is what I have so far:
ds.Tables[3].Columns[2].ReadOnly = false;
List<object> table = new List<object>();
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().Where(s => r[3] == s[0])).ToList();
The ToList() is just for debugging. To summarize, ds.Tables[3].Rows[2] is the column I want to update. ds.Tables[3].Rows[3] contains the key I want to join to.
In the reader, the first column contains the matching key to ds.Tables[3].Rows[3] and the second column contains the data with which I want to update ds.Tables[3].Rows[2].
The error I keep getting is
Unable to cast object of type 'WhereEnumerableIterator1[System.Data.IDataRecord]' to type 'System.IConvertible'.Couldn't store <System.Linq.Enumerable+WhereEnumerableIterator1[System.Data.IDataRecord]> in Quoting Dealers Column. Expected type is Int32.
Where am I going wrong with my LINQ?
EDIT:
I updated the line where the updating is happening
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().First(s => r[3] == s[0])[1]).ToList();
but now I keep getting
Sequence contains no matching element
For the record, the sequence does contain a matching element.
You can use the following sample to achieve the join and update operation. Let's suppose there are two Datatables:
tbl1:
tbl2:
Joining two tables and updating the value of column "name1" of tbl1 from column "name2" of tbl2.
public DataTable JoinAndUpdate(DataTable tbl1, DataTable tbl2)
{
// for demo purpose I have created a clone of tbl1.
// you can define a custom schema, if needed.
DataTable dtResult = tbl1.Clone();
var result = from dataRows1 in tbl1.AsEnumerable()
join dataRows2 in tbl2.AsEnumerable()
on dataRows1.Field<int>("ID") equals dataRows2.Field<int>("ID") into lj
from reader in lj
select new object[]
{
dataRows1.Field<int>("ID"), // ID from table 1
reader.Field<string>("name2"), // Updated column value from table 2
dataRows1.Field<int>("age")
// .. here comes the rest of the fields from table 1.
};
// Load the results in the table
result.ToList().ForEach(row => dtResult.LoadDataRow(row, false));
return dtResult;
}
Here's the result:
After considering what #DStanley said about LINQ, I abandoned it and went with a foreach statement. See code below:
ds.Tables[3].Columns[2].ReadOnly = false;
while (reader.Read())
{
foreach (DataRow item in ds.Tables[3].Rows)
{
if ((Guid)item[3] == reader.GetGuid(0))
{
item[2] = reader.GetInt32(1);
}
}
}

Inner Join a C# datatable and Sql Datatable using linq

I know this is obviously a repeated question to ask but I am unable to figure out the issue, as I am new to LINQ.
Basically I have to matchup for duplicate entry of data while adding multiple records at a time. So, I have a Table in my database that has few rows and then I create DataTable dynamically which is clone(in terms of structure) of that table. Now dtDup is the Database Table, returned as dataset/datatable from a select query, and dupVals is the dynamic clone that is to be cross checked for duplicates
var CommnRows =
from dbA in dtDup.AsEnumerable()
join appB in dupVals.AsEnumerable() on
new {
MonthID = dbA.Field<int>("MonthID"),
UserID = dbA.Field<int?>("UserID"), //nullable int
IsActive = dbA.Field<bool?>("IsActive"), //nullable bit
Gender = dbA.Field<String>("Gender").ToString().ToUpper()
}
equals
new {
MonthID = appB.Field<int>("MonthID"),
UserID = appB.Field<int?>("UserID"),
IsActive = appB.Field<bool?>("IsActive")
Gender = appB.Field<String>("Gender").ToString().ToUpper()
}
select dbA;
So, in case I have some rows returned then (I assume, that above join is correct inner join) this means that there are duplicate rows.
But I am getting an error:
Object reference not set to an instance of an object
at new after equals
I found the issue. I was trying to change the type of one of my column that was a string and at first I did not included in the question (now updated), but when I tried debugging it line by line, I found that it was breaking while near Gender. So, just removed the ToString().ToUpper() from that area and it worked.

Selecting second set of 20 row from DataTable

I have a DataTable I am populating from SQL table with the following example columns
ID
Type
Value
I am populating the DataTable with rows which are of a certain type. I want to select the rows 10 - 20 from my resulting DataTable:
Connect conn = new Connect();
SqlDataAdapter da = new SqlDataAdapter(SQL, conn.Connection());
//Creates data
DataTable d = new DataTable();
da.Fill(d);
DataRow[] result = d.Select();
In the above code I have omitted the main SQL, and currently I have no select for my DataRow array. I cannot find a way to reference the row numbers.
So for instance I am looking for something like Select("rownum > X && rownum < Y")
I have searched here, and a number of other resources to no avail. Any clues would be really handy, or just a simple not possible.
It's always better to select only what you need from the database(f.e. by using the TOP clause or a window function like ROW_NUMBER) instead of filtering it in memory.
However, you can use Linq-To-DataSet and Enumerable.Skip + Enumerable.Take:
var rows = d.AsEnumerable()
.Skip(9).Take(11); // select rows 10-20 as desired (so you want 11 rows)
If you want a new DataTable from the filtered result use CopyToDataTable, if you want a DataRow[] use rows.ToArray().
I would just do the 'take' and 'skip' command and keep it simple:
d.Select().Skip(10).Take(10); // skips 10 rows, then selects ten after that.
This would be assuming you have the Linq using set (using System.Linq)

Is it possible to order data in a datatable by a related datatable in the dataset?

I've been using ORM's for so long, I appear to have forgotten most of my basic data handling skills in dotnet :(
Is it possibly to do something like this?
DataSet ds = new DataSet();
var compiledConnection = new SqlConnection(cDbConnectionString);
SqlDataAdapter daChart = new SqlDataAdapter("select * from Chart", compiledConnection);
daChart.Fill(ds, "chart");
if (ds.Tables["chart"].Rows.Count > 0)
{
var sourceConnection = new SqlConnection(sourceDbConnectionString);
SqlDataAdapter daSource = new SqlDataAdapter("select * from source", sourceConnection);
daSource.Fill(ds, "source");
DataRelation chart_source = new DataRelation("dr", ds.Tables["chart"].Columns["intItemId"],
ds.Tables["source"].Columns["intRowId"], false);
ds.Relations.Add(chart_source);
}
And then use one of the columns in the table "chart" to order the data in the table "source" across the datarelation?
(Before anyone asks, these two tables are in separare instances of SqlServer on separate sites, so just pulling the data as one table is not a straightforward task. Hence this approach)
Cheers,
Matt
That just creates the equivalent of a foreign key. You seem to want the equivalent of an INNER JOIN.
In addition to creating the relationship, it requires adding all the columns of one to the other, loops to fill in the rows and GetParentRows. MS has some good starting point code:
http://support.microsoft.com/kb/326080
EDIT. You could also do a SQL version, by creating a linked server and using 4 part names [server].[database].[owner].[table]
Thanks for the suggestion, but I discovered you can do it with LINQ rather more easily:
DataTable source = ds.Tables["source"];
DataTable chart = ds.Tables["chart"];
var joinedTable =
from s in source.AsEnumerable()
join c in chart.AsEnumerable()
on s.Field<Int64>("intRowId") equals
c.Field<Int64>("intItemId")
select new
{
intRowId = s.Field<Int64>("intRowID"),
strTitle = s.Field<string>("strTitle"),
intWeight = c.Field<Int64>("intWeight")
};
var sortedTable = from j in joinedTable
orderby j.intWeight descending
select j;

Categories