PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

adding a primary key to a dataset/datatable

 
 
Bernie Yaeger
Guest
Posts: n/a
 
      22nd Feb 2005
I have a need to add a primary key to a dataset/datatable. How can this be
done using a standard oledb data provider?

Tx for any help.


 
Reply With Quote
 
 
 
 
Arsalan
Guest
Posts: n/a
 
      22nd Feb 2005
OleDataAdapter should have primary key in its select command/statement,
for e.g Select Employee_ID from employees

Then use fill command to fill the dataset

OleDataAdapter.Fill(Dataset)
thats it, you'll have primary key in ur dataset


"Bernie Yaeger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a need to add a primary key to a dataset/datatable. How can this be
>done using a standard oledb data provider?
>
> Tx for any help.
>



 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      22nd Feb 2005
Hi Arsalan.

I am working with vfp free tables - there are no primary keys in the
original tables and they can't be added to the backend (because they are not
mine and adding a column would screw them up).

Bernie

"Arsalan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> OleDataAdapter should have primary key in its select command/statement,
> for e.g Select Employee_ID from employees
>
> Then use fill command to fill the dataset
>
> OleDataAdapter.Fill(Dataset)
> thats it, you'll have primary key in ur dataset
>
>
> "Bernie Yaeger" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have a need to add a primary key to a dataset/datatable. How can this
>>be done using a standard oledb data provider?
>>
>> Tx for any help.
>>

>
>



 
Reply With Quote
 
Arsalan
Guest
Posts: n/a
 
      22nd Feb 2005
If the table structure doesn't contain primary key then its impossible to
add it to the dataset.
How about making a column alias from two or more columns [like composite
primary key] that would make it possible to select records from the table,
it all depends on what kind of column u have in ur database table

"Bernie Yaeger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Arsalan.
>
> I am working with vfp free tables - there are no primary keys in the
> original tables and they can't be added to the backend (because they are
> not mine and adding a column would screw them up).
>
> Bernie
>
> "Arsalan" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> OleDataAdapter should have primary key in its select command/statement,
>> for e.g Select Employee_ID from employees
>>
>> Then use fill command to fill the dataset
>>
>> OleDataAdapter.Fill(Dataset)
>> thats it, you'll have primary key in ur dataset
>>
>>
>> "Bernie Yaeger" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I have a need to add a primary key to a dataset/datatable. How can this
>>>be done using a standard oledb data provider?
>>>
>>> Tx for any help.
>>>

>>
>>

>
>



 
Reply With Quote
 
Brian Swanson
Guest
Posts: n/a
 
      23rd Feb 2005
I assume you are asking to add a primary key just for processing while
the dataset/datatable is in memory and aren't trying to write it back
out anywhere.

You can add a primary key by using the PrimaryKey property on a
DataTable

You can use the following code as an example:

Dim lds as DataSet = GetSomeDataSet()

For Each ldt As DataTable In lds.Tables
ldt.PrimaryKey = New DataColumn() {ldt.Columns("ColumnName")}
Next

-------

A couple things to note here. This iterates through each datatable
that's returned in the dataset, you can run the line inside the
for..each by itself if you only want to create a PrimaryKey for just one
table.

Also, if you want to make a multi-column primary key then the line
inside the for..each would look something like this:

ldt.PrimaryKey = New DataColumn() _
{ldt.Columns("ColumnName1"), ldt.Columns("ColumnName2")}

Hopefully this makes sense...

Brian Swanson

> > I am working with vfp free tables - there are no primary keys in the
> > original tables and they can't be added to the backend (because they are


 
Reply With Quote
 
Brian Swanson
Guest
Posts: n/a
 
      23rd Feb 2005
One other note...

When adding the primary key in this way the system doesn't automatically
set the column you assign to the primary key as unique. You have to do
this as well by using:

ldt.Columns("ColumnName").Unique = True


---
Brian Swanson


"Brian Swanson" <(E-Mail Removed)> wrote in message
news(E-Mail Removed):
> I assume you are asking to add a primary key just for processing while
> the dataset/datatable is in memory and aren't trying to write it back
> out anywhere.
>
> You can add a primary key by using the PrimaryKey property on a
> DataTable
>
> You can use the following code as an example:
>
> Dim lds as DataSet = GetSomeDataSet()
>
> For Each ldt As DataTable In lds.Tables
> ldt.PrimaryKey = New DataColumn() {ldt.Columns("ColumnName")}
> Next
>
> -------
>
> A couple things to note here. This iterates through each datatable
> that's returned in the dataset, you can run the line inside the
> for..each by itself if you only want to create a PrimaryKey for just one
>
> table.
>
> Also, if you want to make a multi-column primary key then the line
> inside the for..each would look something like this:
>
> ldt.PrimaryKey = New DataColumn() _
> {ldt.Columns("ColumnName1"), ldt.Columns("ColumnName2")}
>
> Hopefully this makes sense...
>
> Brian Swanson
>
> > > I am working with vfp free tables - there are no primary keys in the
> > > original tables and they can't be added to the backend (because they
> > > are


 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      23rd Feb 2005
Hi Brian,

I looked at a couple of books I have on my shelf and figured this out, but
thank you.

Unfortunately, I do want to write it back to the backend, and, as luck would
have it, this does me absolutely no good! (as your caveat suggested). But
thanks for responding.

Bernie

"Brian Swanson" <(E-Mail Removed)> wrote in message
news:%238$(E-Mail Removed)...
> One other note...
>
> When adding the primary key in this way the system doesn't automatically
> set the column you assign to the primary key as unique. You have to do
> this as well by using:
>
> ldt.Columns("ColumnName").Unique = True
>
>
> ---
> Brian Swanson
>
>
> "Brian Swanson" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed):
>> I assume you are asking to add a primary key just for processing while
>> the dataset/datatable is in memory and aren't trying to write it back
>> out anywhere.
>>
>> You can add a primary key by using the PrimaryKey property on a
>> DataTable
>>
>> You can use the following code as an example:
>>
>> Dim lds as DataSet = GetSomeDataSet()
>>
>> For Each ldt As DataTable In lds.Tables
>> ldt.PrimaryKey = New DataColumn() {ldt.Columns("ColumnName")}
>> Next
>>
>> -------
>>
>> A couple things to note here. This iterates through each datatable
>> that's returned in the dataset, you can run the line inside the
>> for..each by itself if you only want to create a PrimaryKey for just one
>>
>> table.
>>
>> Also, if you want to make a multi-column primary key then the line
>> inside the for..each would look something like this:
>>
>> ldt.PrimaryKey = New DataColumn() _
>> {ldt.Columns("ColumnName1"), ldt.Columns("ColumnName2")}
>>
>> Hopefully this makes sense...
>>
>> Brian Swanson
>>
>> > > I am working with vfp free tables - there are no primary keys in the
>> > > original tables and they can't be added to the backend (because they
>> > > are

>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      24th Feb 2005
Bernie,

What is it you want to achieve setting a primary key in the tables of your
foxpro database.
Or use as Brian wrote use it internally by instance to set a relation.

For me it is as well not clear which of the two it is (because I have readed
your previous questions).

Cor


 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      24th Feb 2005
Hi Cor,

I tried it, and it doesn't achieve my goal. I was hoping that it would
somehow enable me to update the back end more effectively, but it did not.

Bernie

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bernie,
>
> What is it you want to achieve setting a primary key in the tables of your
> foxpro database.
> Or use as Brian wrote use it internally by instance to set a relation.
>
> For me it is as well not clear which of the two it is (because I have
> readed your previous questions).
>
> Cor
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      24th Feb 2005
Bernie,

Your goal is not clear in my opinion.

Do you want to set the keys in the database
Or
Do you want to set the primary keys in the dataset.

Where I assume setting the primary keys in the database using the SQL Alter
command and the

http://msdn.microsoft.com/library/de...able___sql.asp

the SQLcommaned.executenonquery to process that.

:-)

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the best way to set the primary key in a datatable or dataset Peted Microsoft C# .NET 1 15th Sep 2008 08:11 AM
Adding a Datatable to a dataset =?Utf-8?B?QW50?= Microsoft ADO .NET 2 30th Jan 2006 02:03 AM
adding a primary key to a dataset/datatable Bernie Yaeger Microsoft VB .NET 10 24th Feb 2005 02:52 PM
Adding Columns to DataSet-DataTable out of Position =?Utf-8?B?QXNodG9u?= Microsoft ADO .NET 2 28th Dec 2004 07:43 PM
Adding a datatable to a dataset Shawn McNiven Microsoft ADO .NET 1 5th Mar 2004 03:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:10 AM.