Creating Database Relationship programmatically - c#

I have two separate databases that have data that coincides with another. However they are not relational.
I'm trying pro-grammatically create a relationship to build some statistics in C#. I'm looking for the number of actions in a case and its assets associated.
From one database I can see what asset belongs to what case:
| 7AU45448 | cases/unchanged/
| 7AI61361 | cases/unchanged/
| 8C52A5A1 | cases/unchanged/
| 8643Y053 | cases/unchanged/
| 8643Y052 | cases/unchanged/
| 8643Y051 | cases/unchanged/
| 8643Y050 | cases/unchanged/
| B4F043RB | cases/ups01/
| B4F043R7 | cases/ups01/
| B4F043R5 | cases/ups01/
| B4F043QZ | cases/ups01/
| B4F043QY | cases/ups01/
| B4F043RA | cases/ups01/
| B4F043R1 | cases/ups01/
| B4F043R8 | cases/ups01/
| B4F043R9 | cases/ups01/
| B4F043QX | cases/ups01/
| B4F043R3 | cases/ups01/
| B4F043QW | cases/ups01/
| B4F043R4 | cases/ups01/
| B4F043RC | cases/ups01/
| B4F043R2 | cases/ups01/
| B4F043R0 | cases/ups01/
| B4F043RD | cases/ups01/
| B4F043R6 | cases/ups01/
The other database is for logs, and holds no information on the case itself. Only the asset and detail are inside.
The information in this database is like:
7AU45448 | Processed file
7AU45448 | Download file
7AU45448 | View file
I can easily do a action count per asset on the database but not on the case. This is why I need the relationship.
If anyone has and Ideas or suggestions please let me know!
Thanks in Advance!

Since your definition of "not relational" was merely meant to be "without constraints", you should be able to compare data in two different databases as long as the field you're joining on is the same data type. Just make sure your left table is the table with the values you care about if you use a LEFT OUTER JOIN. In this case, [db1].[dbo].[table1] is the left table.
Example:
SELECT [db1].[dbo].[table1].*, [db2].[dbo].[table2].*
FROM [db1].[dbo].[table1]
LEFT OUTER JOIN [db2].[dbo].[table2] ON [db1].[dbo].[table1].[field_in_db1_table1] = [db2].[dbo].[table2].[field_in_db2_table2]

Related

WPF Datagrid or maybe something else that can have indents in the first column?

I have a table with a bunch of data this is a small sample:
Header - Object name
Waarde
ExportRoot.a80_02_00_01_Status.Settings.Off
0
ExportRoot.a80_02_00_01_Status.Standby
0
ExportRoot.a80_02_00_01_Status.Cooling
1
ExportRoot.a80_02_00_01_Status.Drying
0
ExportRoot.a80_02_00_01_Status.Heating
0
ExportRoot.a80_02_00_01_Status.PrepDrain
0
I wish to style this table in a way to where its more readable by removing the object parts and indenting the table like how it looks below.
| Header - Object name | Waarde |
|--------------------------|---------|
| ExportRoot | |
| | a80_02_00_01_Status | |
| | | Settings | |
| | | | Off | 0 |
| | | | Standby | 0 |
| | | | Cooling | 1 |
| | | | Drying | 0 |
| | | | Heating | 0 |
| | | | PrepDrain | 0 |
Is there a way of doing this and if so what should I be looking for all I've found so far are tables with sub tables and that not exactly what I'm looking for. If anyone has an idea let me know I'm super lost.
So basically you need a TreeListView:
There is one project on CodeProject that you can check:
WPF TreeListView Control
There is also commercial products, like DevExpress:
DevExpress TreeList View

Notify in .NET application if SQL data has changed

I have got two tables. Table A contains the main data in every change state, Table B has the detail data.
Table A
| ID | Name |
| 1 | House |
| 2 | Tree |
| 3 | Car |
Table B
| ID | FK | DateTime | Color | Type |
| 1 | 1 | 2017-26-01T13:00:00 | Red | Bungalow |
| 2 | 2 | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | 1 | 2017-26-01T13:10:00 | Green | Bungalow |
| 4 | 3 | 2017-26-01T13:15:00 | Yellow | Smart |
| 5 | 1 | 2017-26-01T13:20:00 | White | Bungalow |
| 6 | 3 | 2017-26-01T13:25:00 | Black | Smart |
Result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T13:25:00 | Black | Smart |
The current state of an entity in Table A is described by the record with the youngest timestamp in Table B.
Now, I want to be nofitied, if an entity gets a new state (new record in Table B) or a new entity is created (new records in Table A and B), i.e. it could look like.
New result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T19:25:00 | Silver | Smart |
| 4 | Dog | 2017-26-01T20:25:00 | White / Black | Poodle |
With SqlDependency it is not possible to be notified for a statement which contains GROUP BY with MAX aggregate, window functions or TOP clause. So I have no idea how I can get the last detail data for an entity.
Is there any possibility to create a statement for this requirement or are there any other ways to be notified after changes for this result?
You can create and use SQL Triggers usin CLR
Here - SQL Trigger CLR

MS SQL Table structure for dynamic app

I am building a dynamic app.
The basic of the app is i can create a question. A question may have many columns. These columns will be displayed in table structure. Then the user will fill the details. Below is the sample.
Question:
1. List the projects available and their deadlines?
Columns:
1. Project - Textbox
2. Deadline - Textbox
So i will show this data in the table like below.
The user will fill the row and click on submit button. Another empty row will get added to add the another response like below.
The user will submit any no of responses.
Like this the no of columns will be dynamic and also the each column will have some validation like Unique,AcceptsNull etc...
How to create table structure in ms sql server to save the question,column and the responses.
Please help thanks in advance..
Your scenario is similar with the way in which Microsoft Project Server stores its data in MSSQL. In MSPE for each project, a task can have a dynamic number of columns. In your case, if you can make all your column types as VARCHAR, than your scenario simplifies even more.
The proposed structure would be like this:
This is the table with the questions:
tbl_Questions
+------------------------------------------------------------------+
| Id | Question |
+----------------------------------------------------------------- +
| RandomGUID1 | "List the projects available and their deadlines?" |
| RandomGUID2 | "List the projects available?" |
+------------------------------------------------------------------+
tbl_RelationTable
+---------------------------------------------------------------------------+
| Id | QuestionId | Column1 | Column2 | ... |Column 1000 |
+---------------------------------------------------------------------------+
| 1 | RandomGUID1 | RandomGUID111 | RandomGUID112 | | null |
| 2 | RandomGUID2 | RandomGUID113 | null | | null |
+---------------------------------------------------------------------------+
In this table you store all relations between tbl_Questions and the other tables. You define in this table a sufficient number of columns, here I proposed 1000, but maybe in your case 10-15 columns would be enough. One important aspect is that you need to use GUIDs for Question ID in order to be unique.
Now we define the real data tables.
This is the table where you define your answer columns:
tbl_AnswerColumns
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | ColumnName |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | Title |
| 2| RandomGUID112 | RandomGUID1 | Answer |
| 3| RandomGUID113 | RandomGUID2 | Title |
+-----------------------------------------------+
This is the table where you store the answers values:
tbl_AnswerValues
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | Answer |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | "Answer1" |
| 2| RandomGUID112 | RandomGUID1 | "Answer2" |
| 3| RandomGUID113 | RandomGUID2 | "Answer3" |
+-----------------------------------------------+
You need to define foreign keys in all tables in order to make the data retrieval much faster. This is why each table should contain a foreign key to the QuestionId.
By the information you've given I'm thinking something like this?
Questions
+---------------------------------------------------------+
| Id | Question |
+----+--------------------------------------------------- +
| 1 | "List the projects available and their deadlines?" |
+---------------------------------------------------------+
Answers
+-----------------------------------------------------------------------------------+
| Id | QuestionId | Order | Title | Anwser | Unique | Nullable | Values |
+----+------------+-------+------------+-------------+--------+----------+----------+
| 1 | 1 | 0 | "Project" | "Project 1" | True | False | "P1, P2" |
| 2 | 1 | 1 | "Deadline" | "Apr-15" | False | True | NULL |
+-----------------------------------------------------------------------------------+
Edit: I've added the validations. If you want the same validations for all anwers for the same question, you can also move the validation columns to the Questions-table.

merge single table column with different types

Table Columns
or_cv_no
here is where 'cv_no' and 'or_no' stored
type
different types are 'CV' and 'OR'
amount
here is where 'cv_amt' and 'or_amt' stored
date
data that is being fetched has same date
my query is this
SELECT IIF(type = 'CV', or_cv_no) AS cv_no, IIF(type = 'CV', amount) AS cv_amt, IIF(type = 'OR', or_cv_no) AS or_no, IIF(type = 'OR', amount) AS or_amt FROM transacAmt
and the output is
how can i output it to look like this sample:
| cv_no | cv_amt | or_no | or_amt |
|---------------------------------|
| 1234 | 1,500 | 4321 | 1,200 |
|-------+--------+-------+--------|
| 7777 | 1,700 | 2222 | 1,760 |
|-------+--------+-------+--------|
| 1111 | 1,900 | 3333 | 1,530 |
|-------+--------+-------+--------|
| | | 5355 | 1,420 |
|-------+--------+-------+--------|
| | | 1355 | 4,566 |
EDIT: The output must be like this: marked 'x' must be removed.. or be filled by data

What kind of format is this? Cisco WIM Chat Transcript

O{bTyp | S{WCS-STD}bUsr | S{Paul R Dunaway}bUid | S{1350}sCmd | S{}sId | A{}sNme | S{}sUrl | S{}sLbl | S{}sCok | S{}mMsg | S{All Other Software}fAct | S{}fTyp | S{}fKey | S{}fVal | S{}bUserType | S{CUST}transType | S{}mTsp | S{2012-01-26 15:03:04}}|O{bTyp | S{WCS-STD}bUsr | S{system}bUid | I{-1}sCmd | S{}sId | A{}sNme | S{}sUrl | S{}sLbl | S{}sCok | S{}mMsg | S{[An agent will be with you shortly.]}fAct | S{}fTyp | S{}fKey | S{}fVal | S{}bUserType | S{SYSTEM}transType | S{}mTsp | S{2012-01-26 15:03:04}}
This is used by our webchat system(Cisco UCCE/eGain) to store transcripts. I am looking to access them via C#/SQL but I am finding this encoding to be a bit weird. the above is after I cleaned out all the URL encoding bits example below
O%7BbTyp%20%7C%20S%7BWCS-STD%7DbUsr%20%7C%20S%7BPaul%20R%20Dunaway%7DbUid%20%7C%20S%7B1350%7DsCmd%20%7C%20S%7B%7DsId%20%7C%20A%7B%7DsNme%20%7C%20S%7B%7DsUrl%20%7C%20S%7B%7DsL
It seems to make a little more sense reindented:
O{
bTyp | S{WCS-STD}
bUsr | S{Paul R Dunaway}
bUid | S{1350}
sCmd | S{}
sId | A{}
sNme | S{}
sUrl | S{}
sLbl | S{}
sCok | S{}
mMsg | S{All Other Software}
fAct | S{}
fTyp | S{}
fKey | S{}
fVal | S{}
bUserType | S{CUST}
transType | S{}
mTsp | S{2012-01-26 15:03:04}
}
|
O{
bTyp | S{WCS-STD}
bUsr | S{system}
bUid | I{-1}
sCmd | S{}
sId | A{}
sNme | S{}
sUrl | S{}
sLbl | S{}
sCok | S{}
mMsg | S{[An agent will be with you shortly.]}
fAct | S{}
fTyp | S{}
fKey | S{}
fVal | S{}
bUserType | S{SYSTEM}
transType | S{}
mTsp | S{2012-01-26 15:03:04}
}
(Please excuse me if I'm just restating the obvious.) This doesn't look like any form of serialization I've seen before; I don't think it's outside the realm of possibility to think it's proprietary. Values, contained within braces, can be of one of four data types, indicated by the character immediately preceding the opening brace: S for string, I for integer, O for object (a collection of values as named properties), or A, presumably for array. Object properties are named based on the string preceding the value and separated from it with a pipe character.
Some questions still remain:
What does an array value look like?
Why does the data type for bUid change from one object to the next?
Why is the whitespace so inconsistent? (I would assume whitespace is ignored when not part of the value.)
Why is a pipe character used to separate the two top-level objects? Is this how the array notation works?

Categories