Re: Create a multi-column primary key from C++
- From: theunissen@xxxxxxxxxxx
- Date: Thu, 10 Apr 2008 10:54:57 -0700 (PDT)
On Apr 10, 10:16 am, "Bob Barrows [MVP]" <reb01...@xxxxxxxxxxxxxxx>
wrote:
theunis...@xxxxxxxxxxx wrote:
Hi,
I am using SQL Server 2005 Express and I am able to create a primary
key using the following code in Visual C++ 2005:
----------------------------------------------------------------------
--------------------> m_pTable->Keys->Append("MyKey", ADOX::adKeyPrimary, "Label", _T(""),
_T(""));
----------------------------------------------------------------------
--------------------> "Label" is the name of the column that should contain the primary key.
Hwoever, when I want to create a multi-column key hopwever, an
exception is thrown telling me that the parameters are incorrect. The
folowing code show how I am creating the multi-column key. The fail
occors in the append function.
----------------------------------------------------------------------
--------------------> ADOX::_KeyPtr m_pKey = NULL;
m_pKey.CreateInstance(__uuidof(ADOX::Key));
m_pKey->PutName("MyKey");
m_pKey->PutType(ADOX::adKeyPrimary);
m_pKey->Columns->Append("Label", ADOX::adVarChar, 255);
Table.m_pTable->Keys->Append(_variant_t((IDispatch *) m_pKey),
ADOX::adKeyPrimary, "", "", "");
m_pKey.Release();
m_pKey = NULL;
----------------------------------------------------------------------
--------------------
I have no clue what I'm doing wrong. It might have todo with the
optional parameters in the append method. Though I think they are just
optional since I'm passing a Key object, so it shouldn't matter what
you specify in C++.
Why not simply run an ALTER TABLE ... CONSTRAINT statement via a Command
object? I've never used ADOX to do this so I would have to resort to the
same documentation you are undoubtedly reading.
Here is an example that I generated using SSMS:
ALTER TABLE dbo.wrkPO_Details ADD CONSTRAINT
PK_wrkPO_Details PRIMARY KEY CLUSTERED
(
PURCHASE_ORDER_NUMBER,
LINE_NUMBER
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
Simply alter it to contain the details of your table and key columns,
then execute if via a Command object.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Thanks,
An additional problem is that it needs to work for both: MSACCESS and
SQL Server. So I probably cannot use TSQL statements and have to do
everything through ADO and ADOX.
There seems to be quite a few differences between MSAccess and SQL
Server. SQL server is more strict in the way tables are created. the
order of adding fields and adding tables and keys seem to be important
between MsAccess and Sql server.
All the examples show Visual Basic code, which is much easier to use,
and some of the functionality is done 'under-the-hood', which is
probably what causes my problems since I cannot see or figure out how
to do it.
.
- Follow-Ups:
- Re: Create a multi-column primary key from C++
- From: Bob Barrows [MVP]
- Re: Create a multi-column primary key from C++
- References:
- Create a multi-column primary key from C++
- From: theunissen
- Re: Create a multi-column primary key from C++
- From: Bob Barrows [MVP]
- Create a multi-column primary key from C++
- Prev by Date: Re: Create a multi-column primary key from C++
- Next by Date: Re: Create a multi-column primary key from C++
- Previous by thread: Re: Create a multi-column primary key from C++
- Next by thread: Re: Create a multi-column primary key from C++
- Index(es):
Relevant Pages
|
|