Re: Create a multi-column primary key from C++



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.
.



Relevant Pages

  • RE: SQL Server Express on SBS2003 standard
    ... This newsgroup only focuses on SBS technical issues. ... you may want to contact Microsoft CSS directly. ... After you applied the SBS 2003 SP1 successfully, you can install the ... SQL Server Express on SBS. ...
    (microsoft.public.windows.server.sbs)
  • Re: Accessing sql server
    ... port, select All ports, in Remote port, select Fixed port and input 1433 ... Microsoft CSS Online Newsgroup Support ... This newsgroup only focuses on SBS technical issues. ... | I have now checked and I can access the sql server when creating an ODBC ...
    (microsoft.public.windows.server.sbs)
  • Re: Accessing sql server
    ... Filters rule to see if we can resolve this issue. ... Microsoft CSS Online Newsgroup Support ... This newsgroup only focuses on SBS technical issues. ... | I have now checked and I can access the sql server when creating an ODBC ...
    (microsoft.public.windows.server.sbs)
  • RE: Allow access to external SQL server via ODBC thru ISA 2004
    ... Thank you for posting in SBS newsgroup. ... SQL server though SBS with ISA2004. ... To make the client access normally to external ODBC source, ... configure a protocol definition and a access rule on ISA. ...
    (microsoft.public.windows.server.sbs)
  • RE: SQL Server Agent Logon Issue (SBS 2003)
    ... The service did not start due to a logon failure. ... the SQL Server startup service account or the SQL Server Agent startup ... service account is not correct. ... Microsoft CSS Online Newsgroup Support ...
    (microsoft.public.windows.server.sbs)