SQLite - joining using Guid return empty - c#

I have the following SQLite query (using C#):
command.CommandText = #"SELECT
sd.productuid,
sp.productuid,
sp.description as productname
FROM sale_detail sd
JOIN setup_product sp ON 1 = 1";
In this query I get all both tables to see exactly the values of my productuid column.
If you see the result you can verify that the productuid in one table exists in the other:
Now, I do the right join:
command.CommandText = #"SELECT
sd.productuid,
sp.productuid,
sp.description as productname
FROM sale_detail sd
JOIN setup_product sp ON sp.productuid = sd.productuid";
And is empty. This is breaking my head, I don't know why the join is not working even when the productuid in table sale_detail exists in setup_product.
Both fields are uniqueidentifier.
Why is this not working?

Related

Multiple joins in SQL statement

To clear things up, I have two tables, employees and jobs.
In employees I have columns firstname, lastname, employees id.
In jobs I have columns
job_id, job_name, job_description, job_opener, job_responsible_person
where the last two are foreign keys which refer to the employees table.
This is my SQL statement
String cmd = "SELECT je.id, firstname, lastname, job_name FROM jat.employees je" +
" INNER JOIN jat.jobs jj ON je.id = jj.job_opener ";
And I fill data table with it.
What I need is adding two more columns, like second first name and second last name, in same table, but which will depend on job_responsible_p, so ON clause would be
ON je.id = jj.job_responsible_p
Is that possible in one query?
Yes, you can join several table at once:
SELECT je.id, je.firstname, je.lastname, jj.job_name, jr.firstname, jr.lastname
FROM jat.employees AS je INNER JOIN jat.jobs AS jj INNER JOIN jat.employees AS jr
ON je.id=jj.job_opener AND jr.id=jj.job_responsible_person;
Yes it is since you can use conditional operator like AND OR with JOIN ON condition.
INNER JOIN jat.jobs jj
ON je.id=jj.job_opener
AND je.id=jj.job_responsible_p

field not found according to npgsql, but it exists according to pgadmin

I have this line of code as my query in C#:
cmd.CommandText = "SELECT *
FROM product
LEFT JOIN category ON product.category_id = category.id
WHERE product.id = #productId";
The product table has a column called name which I need.
This is the line I use in my application to retrieve it.
product.ProductName = reader.GetString(reader.GetOrdinal("\"product\".\"name\""));
The error I'm getting is
System.IndexOutOfRangeException: Field not found
on that line.
reader.GetOrdinal("name");
Firstly the resultset does not have a field named "product"."name", but rather one named "name". Consider that if you were to try to select from that resultset within PostgreSQL it would be the same case:
SELECT "product"."name" FROM
(SELECT *
FROM product
LEFT JOIN category ON product.category_id = category.id
WHERE product.id = #productId) subquery
Doesn't work, but:
SELECT "name" FROM
(SELECT *
FROM product
LEFT JOIN category ON product.category_id = category.id
WHERE product.id = #productId) subquery
Does.
Secondly, don't use the PostgreSQL escaping on the name of the field.

Read Schema Name, Table Name and Column Name From a SQL DB and return to C# Code

I have a DB (in SQL Server 2008 SP3) and need all Schema names, Table names and Column Names in related hierarchy in C# Code, I have the SQLElement Class as Following:
public class SQLElement
{
public string SchemaName { get; set; }
public string TableName { get; set; }
public string ColumnName { get; set; }
}
And have a List Like:
List<SQLElement> SQLElementCollection = new List<SQLElement>();
So how can I read Names from DB and add them to this List (SQLElementCollection)?
for example assume we create a table like this:
Create Table [General].[City] (
[Id] BIGINT NOT NULL IDENTITY(1, 1),
[Title] NVARCHAR(30) NOT NULL DEFAULT (N''),
[Province_Id] BIGINT NOT NULL
)
and I need the list like:
[0]={SchemaName="General", TableName="City", ColumnName="Id"}
[1]={SchemaName="General", TableName="City", ColumnName="Title"}
[2]={SchemaName="General", TableName="City", ColumnName="Province_Id"}
Does any one have any idea about this?
Edit:
In next step how we can get the type of each column or related properties?
My suggestion is to include another member DataType in SQLElement if you have change permission or create another class with a property name DataTypeand then inherit from SQLElement and then save data type name into it for later use and use below query for all information, thanks
SELECT t.name AS TableName,
SCHEMA_NAME(t.schema_id) AS SchemaName,
c.name AS ColumnName,
tp.name as DataType
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN sys.types tp ON c.system_type_id = tp.system_type_id
ORDER BY TableName;
Connect to your database and execute the following statement:
select *
from INFORMATION_SCHEMA.COLUMNS
order by TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION;
Check the results and then peek and choose what you need.
This query will give you all column names and schema name
SELECT t.name AS tblName,
SCHEMA_NAME(schema_id) AS [schemaName],
c.name AS colName
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
where SCHEMA_NAME(schema_id) = 'dbo' // you can include this where clause if you want to add additional filter to the result set, like query only tables that belong in particular db schema, or query only tables that starts with particular name (maybe prefix or sufix), etc.
ORDER BY tblName;
you have to execute the above query and take results in list
Have a look at the Information_Schema Views in your DB. These have all the stuff you want already in them.

Select from two tables inside an insert statement in sql

i am trying to insert a statement contains WHERE from two different tables :
the table i want to insert into is dbo.order
the other two tables are :
dbo.users. user_id.
dbo.packages. package_id.
another order field "notes".
the statement i tried is
insert into dbo.order
(customer_id,package_id,notes)
Select user_id,Package_ID
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner','notes value here';
any suggestions ?
There's no obvious join here so you'll get the cartesian product of Bader orders and beginner packages. Not sure what the notes value should be. If its a literal you can just include it in the select clause.
insert into dbo.order
(customer_id,package_id,notes)
Select
user_id,Package_ID , 'notes value here'
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner';
insert into dbo.order
(customer_id,package_id,notes)
Select user_id, Package_ID, 'notes value here'
from
dbo.users, dbo.packages
where
username = 'bader' AND package_name = 'beginner';

C# datatable from sql join 2 columns same name

I have a sql select like so which returns a datatable:
select * from table1 a join table2 b on a.id=b.id
Both table1 and table2 have a column named itemno .
How do i reference the 2 separate columns? Normally I would use something like:
datarow["itemno"].ToString(); <=first column named itemno only
datarow["b.itemno"].ToString(); <= fail
However this only seems to get the first column named itemno.
Is there a way to reference the second column named itemno without changing my sql statement? (I know i can change my sql statement, take out the * and put in column aliases).
You can reference the columns by index instead:
datarow[0].ToString();
I'd much prefer aliasing them though to be honest.
Given an SQL query like this
select a.id, b.id, a.columnA,a.columnB,a.itemno,b.itemno
from table1 a
join table2 b on a.id=b.id
Your C# code would/could look like this to read all rows and all columns:
using (SqlCommand getAllColumns = new SqlCommand("select a.id, b.id,a.columnA,a.columnB,a.itemno,b.itemno from table1 a join table2 b on a.id=b.id", conn))
{
using (var drreader = getAllColumns.ExecuteReader())
{
DataTable tb = new DataTable();
tb.BeginLoadData();
tb.Load(drreader);
tb.EndLoadData();
foreach(DataRow row in tb.Rows.Cast<DataRow>().ToList())
{
// assuming these are all varchar columns
string idA = (string)row["id"];
string idB = (string)row["id1"];
string columnA = (string)row["columnA"];
string columnB = (string)row["columnB"];
string columnAItemNo = (string)row["itemno"]; //fetches the first itemno column, a.itemno in this case
string columnBItemNo = (string)row["itemno1"]; //fetches the second itemno column, b.itemno in this case
}
}
}
I use this on .NET Framework 4.5. If you want to verify or debug this, put a breakpoint on the foreach line and inspect the DataTable object. The second itemno column should be titled differently compared to the first one.

Categories