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 ?
Related
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)}
I am converting an old 2010 Access data base to Access 2016, the database being originally built in Access 2000. My customer table used an autonumber field to generate a CustomerId number.
I used this field to connect to my repairs table which currently has 13,000 records. I have not been able to find away to maintain this field in new database, so I thought if I can export the table to an Excel file than add numbers in sequence of missing ID numbers along with data to fill name and address fields for temp import to new database, after import I can delete these fields.
I don't know if there are other ways to maintain this id. I imported customer table with Old ID field name, but access won't allow me to use this field to create relationships to new repairs table.
The ID numbers go from 1 to 8,437 yet there are only 7,884 records in the spreadsheet.
The best way to resequence the IDs is to take advantage of "Cascade Update Related Fields".
What you will have to do is
Delete all the relationships to your table
Change the ID field to Number - Long Integer
Add the relationships back, inforcing cascading updates
Shift all the indices to a number larger than the current max ID
Renumber than starting at 1
Delete all the relationships
Delete the ID field
Save the table
Add the ID field back as auto-increment - primary key
Add the relationships back
ReImcrementCustomerIDs:Sub
Option Compare Database
Option Explicit
Public Sub ReImcrementCustomerIDs(ByVal FirstIndex As Long)
Dim rs As DAO.Recordset
Dim db As Database
Dim SQL As String
Dim ID As Long
Set db = CurrentDb
SQL = "SELECT CustomerID FROM Customers ORDER BY CustomerID;"
Set rs = db.OpenRecordset(SQL, dbOpenDynaset)
Do While Not rs.EOF
rs.Edit
rs("CustomerID") = FirstIndex
FirstIndex = FirstIndex + 1
rs.Update
rs.MoveNext
Loop
End Sub
Usage
ReImcrementCustomerIDs 50000
ReImcrementCustomerIDs 1
I want to insert or update data to a table. The column "Group" is UNIQUE and the ID of the group should remain constant.
there Is a Fiddle:
http://sqlfiddle.com/#!17/551ea/3
on Insert, everything is okay
also the Update works for "Group" = 'TEST01'
But when I insert a new group and then update, the ID changes (press multiple "Run SQL")
This is my insert query:
INSERT INTO GROUPS ("GROUP", SERVER, PATH, SHARE)
VALUES ('TEST04', 4, 4, 4)
ON CONFLICT("GROUP") DO UPDATE
SET SERVER = 11,
PATH = 11,
SHARE = 11
WHERE GROUPS."GROUP" = 'TEST01'
The ID will be used in other tables, this should only be created once for the first entry.
and this is the general structure:
CREATE SEQUENCE gid START 1;
CREATE TABLE GROUPS (
ID integer NOT NULL DEFAULT nextval('gid') PRIMARY KEY,
"GROUP" VARCHAR NOT NULL UNIQUE,
SERVER integer,
PATH integer,
SHARE integer
);
Look at this fiddle
Each time there is a conflict on insert - the sequence value is discarded and ON UPDATE requests a new value. So initially you start with 1, then you insert 3 tuples so the final value of the sequence would be 3. Then you try to insert a new tuple but there is a conflict - so the value of sequence is now 4. Then you try to insert a new tuple - and it gets a value of 5 for the sequence.
If you continue to run the 2 inserts the sequence will continue to increment. SQLfiddle probably uses persistent connections or some connection pooling which does not properly reset the sequence when rebuilding the schema.
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.
Anyone have some good examples (newb worthy) of what bulk inserting should look like or a great way to explain it, utilizing the whole LINQ to SQL method?
I've got a DB with about 7 tables I'm trying to link some .net pages to and as much as I can qry most of these tables, I'm having a heck of a time inserting into them. One complex scenerio I am finding revolves around the use of a GUID and how to get that particular GUID to propogate over into another table...
Anyone have any ideas/examples?
You might want to give a little more insight to the nature of your problem and your current setup.
I haven't really messed with LINQ before so i just set up a really simple project. I was able to insert GUIDs into two seperate tables just fine.
Dim db As New testDBDataContext
' Table1 ID (Primary Key)
Dim gId As Guid = Guid.NewGuid()
' Table2 ID (Primary Key)
Dim gId2 As Guid = Guid.NewGuid()
' Insert Record into Table 1
Dim tb1Insert As New test_tb1 With {.Id = gId, .Name = "TestName"}
db.test_tb1s.InsertOnSubmit(tb1Insert)
' Insert Record into Table 2, with testID referenced to Table1's Primary Key
Dim tb2Insert As New test_tb2 With {.Id = gId2, .test1Id = gId, .OtherName = "OtherName"}
db.test_tb2s.InsertOnSubmit(tb2Insert) '
' Commit Changes
db.SubmitChanges()
The only way to get i got an error is if i set up a relationship in SQL Server between the two tables. Then if i tried to insert table 2 first it would error out because i would be trying to insert an GUID table 2's "test1ID" w/o having that GUID in table 1 yet.
If you are getting a "conflicted with COLUMN FOREIGN KEY constraint" error thats probably what is happening.