How to set primary key on table in a dataset - vb.net

Below I load a dataset, which I believe is table 0. Then I set the name of table 0 in the dataset to PickTable:
PickDataSet.ReadXml(My.Computer.FileSystem.SpecialDirectories.MyDocuments & Pickfile)
PickDataSet.Tables(0).TableName = "PickTable"
I'm having trouble now finding syntax to set the primary key for the PickTable. Column 0 is unique and always has a value. I need to make this the primary key.
I tried something like this:
PickDataSet.Tables("PickTable").PrimaryKey = PickDataSet.Tables("PickTable").Columns(0);

The .PrimaryKey property of DataTable expect an array of DataColumn objects. So you can set the first column as primary key with the following line:
PickDataSet.Tables("PickTable").PrimaryKey = {PickDataSet.Tables("PickTable").Columns(0)}
'or
PickDataSet.Tables("PickTable").PrimaryKey = New DataColumn() {PickDataSet.Tables("PickTable").Columns(0)}

Related

Moving data from one column to another in PostgreSQL

Sometimes, one might want to move some data from one column to another. By moving (in constrast to copying), I mean that the new column was originally null before doing the operation, and the old column should be set to null after doing the operation.
I have a table defined as such:
CREATE TABLE photos(id BIGSERIAL PRIMARY KEY, photo1 BYTEA, photo2 BYTEA);
Suppose there is an entry in the table where photo1 contains some data, and photo2 is NULL. I would like to make an UPDATE query wuch that photo1 becomes NULL and photo2 contains the data that was originally in photo1.
I issue the following SQL command (WHERE clause left out for brevity):
UPDATE photos SET photo2 = photo1, photo1 = NULL;
It seems to work.
I also tried it this way:
UPDATE photos SET photo1 = NULL, photo2 = photo1;
It also seems to work.
But is it guaranteed to work? Specifically, could photo1 be set to NULL before photo2 is set to photo1, thereby causing me to end up with NULL in both columns?
As an aside, this standard UPDATE syntax seems inefficient when my BYTEAs are large, as photo2 has to be copied byte-by-byte from photo1, when a simple swapping of pointers might have sufficed. Maybe there is a more efficient way that I don't know about?
This is definitely safe.
Column-references in the UPDATE refer to the old columns, not the new values. There is in fact no way to reference a computed new value from another column.
See, e.g.
CREATE TABLE x (a integer, b integer);
INSERT INTO x (a,b) VALUES (1,1), (2,2);
UPDATE x SET a = a + 1, b = a + b;
results in
test=> SELECT * FROM x;
a | b
---+---
2 | 2
3 | 4
... and the ordering of assignments is not significant. If you try to multiply-assign a value, you'll get
test=> UPDATE x SET a = a + 1, a = a + 1;
ERROR: multiple assignments to same column "a"
because it makes no sense to assign to the same column multiple times, given that both expressions reference the old tuple values, and order is not significant.
However, to avoid a full table rewrite in this case, I would just ALTER TABLE ... ALTER COLUMN ... RENAME ... then CREATE the new column with the old name.

Vb.net get primary key after inserting row in table adapter

Ok, I'm trying to simply add a new record to an access database in vb.net (ole.db).
I have a dataset and I have configured the data table.
After adding a row like this:
Dim newRow As dataSet.MealsRow = dataSet.Meals.NewMealsRow
newRow.mealName = "name"
newRow.mealType = 12
dataSet.Meals.AddMealsRow(newRow)
tableAdapter.Update(dataSet.Meals)
Dim tableId As Integer = newRow.Id
The tableId is always returing an incorrect primary key value.
If I leave the AutoIncrementSeed to -1 and AutoIncrementStep to -1 in the Data Table, then the tableId will have negative values starting from -1 and going down, -2, -3, etc...
Shouldn't it picking up the correct key value inserted there ?

Error setting primary key in datatable

I am having trouble in setting primary key for a datatable with existing column. I am getting this exception
These columns don't currently have unique values.
The code I am using for the same is like,
dtTemp.PrimaryKey = New DataColumn() {dtTemp.Columns("mycolumn")}
where mycolumn is an existing column in datatable dtTemp with rows like
501,502,503,...901,902,903
(doesn't have unique values)
Note:
The same code with same random values in datatable column works well in one of the similar projects. Any directions would be helpful.
try setting the EnforceConstraints property to False
example -
dtTemp.EnforceConstraints = False
dtTemp.PrimaryKey = New DataColumn() {dtTemp.Columns("mycolumn")}

Update fails with duplicate row

I am teaching myself SQL with a small database I inherited. While I was playing with Update(),
update a
from sls_ord_fact a, sls_ord_dim b
set cust_acct_key = b.cust_acct_key
where a.sls_ord_key = b.sls_ord_key
and a.sls_ord_key <> 0
and b.cust_acct_key <> 0
and a.cust_acct_key <> b.cust_acct_key;
I triggered this error :Duplicate row error in SLS_ORD_FACT
How can identify the duplicate record??? I don't know the data very well.....
It looks like you're trying to set the primary key of the SLS_ORD_FACT table (presumably that is cust_acct_key), and the new value you're trying to set it to already exists in the table. Since primary keys must be unique, this causes an error. The duplicate key is in b - in fact, it's b.cust_acct_key, which is probably a foreign key to SLS_ORD_FACT.cust_acct_key.

Retrieving Database Schema Linq to SQL VB.Net

I want to retrieve each column name and data type then check if the columns is foreign key then query they key table of that relation !!! could be done ?? I googled for 3 days I know that I have to use Mappping model OR Reflection or both ,,,, but i cant do it .
I will simplify what i need assuming :
TABLE1 hase foreign key( COL3) refer to the primary key (COL1) in TABLE0 :
iterate TABLE1 Columns check EACH columns if it is a foreign key ( also get its data type)
Get the relation to determine the associated table(TABLE0)
retrieve the primary key tables (TABLE0)
I got it
I make a function that return the type of each foreign key and the related table class type
Private Function GetForeignKeyTables(ByVal myTableType As Type) As List(Of myForeignKeys)
Dim myDx = New Tester.DataClasses1DataContext
Dim mymodel As New AttributeMappingSource
Dim myAsociations = mymodel.GetModel(GetType(DataClasses1DataContext)).GetTable(myTableType).RowType.Associations
Dim asc = From m In myAsociations Where m.IsForeignKey
Select New myForeignKeys With {.KeyDataType = m.ThisKey.First.DbType, .RelatedTableType = m.OtherType}
Return asc.ToList
End Function
Private Class myForeignKeys
Property KeyDataType As String
Property RelatedTableType As MetaType
End Class
But I still need to retrieve the data from those related table .
I mean how to create an instance of the class from its MetaType variable?