scanhelp
asked on
CDaoDatabase And MS SQL
I want to connect to and update an MS SQL database in C++. Where a user will fill out a windows form and give me the DSN, table, and column. I will then take that info behind the scenes and insert some data to the database.
What I want to avoid is showing any windows dialog box besides my own.
I have a bunch of code that writes data to a DNS-Less MS Access database, but I want to expand that to work with MS SQL.
Thanks!
What I want to avoid is showing any windows dialog box besides my own.
I have a bunch of code that writes data to a DNS-Less MS Access database, but I want to expand that to work with MS SQL.
Thanks!
ASKER
Dan,
Thanks for the reply.
If your read my orginal question I stated that I will allow for a DSN connection for SQL Server... but that I currently have code for DNS-Less connection for Access.
So I am wanting a way to connect to a SQL server with a DSN.
Thanks for the reply.
If your read my orginal question I stated that I will allow for a DSN connection for SQL Server... but that I currently have code for DNS-Less connection for Access.
So I am wanting a way to connect to a SQL server with a DSN.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Alternatively you may refer this example at codeproject :
http://www.codeproject.com/database/sqlconnect.asp <=====
MAHESH
http://www.codeproject.com/database/sqlconnect.asp <=====
MAHESH
ASKER
Dan,
Another good post. But as my title said (and I should have been clearer), I'd like a solution with CDaoDatabase, not CDatabase. Now I could convert my Dao code to CDatabase, but I think it would be easier to work with htat instead.
The rest of you that posted, not much help in the links you provided.
Another good post. But as my title said (and I should have been clearer), I'd like a solution with CDaoDatabase, not CDatabase. Now I could convert my Dao code to CDatabase, but I think it would be easier to work with htat instead.
The rest of you that posted, not much help in the links you provided.
ASKER
Dan,
You pointed me in the right direction, so you get the points.
But here is my final code for this to work:
CString Sql;
AfxGetModuleState()->m_dwV ersion = 0x0601; // Needed for Access 2000 and doesn't seem to hurt anything else.
AfxDaoInit();
CString sConnect;
CString sDSN= "testds";
CString sUser="test";
CString sPswd="test";
sConnect.Format("DSN=%s;UI D=%s;PWD=% s",
(LPCSTR) sDSN,
(LPCSTR) sUser,
(LPCSTR) sPswd
);
db.Open("",FALSE,FALSE,_T( sConnect)) ;
Sql = "SELECT * FROM ";
Sql.Insert(Sql.GetLength() , m_pSettings->struDatabaseI nfo.strTab le); //Finishes building my SQL string.
recset.Open(AFX_DAO_USE_DE FAULT_TYPE ,Sql,dbSee Changes); // I am assuming, would love a comment on this, that I can just put "test" (which is the table I am using) instead of AFX_DAO_USE_DEFAULT_TYPE.
Thanks for your help.
You pointed me in the right direction, so you get the points.
But here is my final code for this to work:
CString Sql;
AfxGetModuleState()->m_dwV
AfxDaoInit();
CString sConnect;
CString sDSN= "testds";
CString sUser="test";
CString sPswd="test";
sConnect.Format("DSN=%s;UI
(LPCSTR) sDSN,
(LPCSTR) sUser,
(LPCSTR) sPswd
);
db.Open("",FALSE,FALSE,_T(
Sql = "SELECT * FROM ";
Sql.Insert(Sql.GetLength()
recset.Open(AFX_DAO_USE_DE
Thanks for your help.
The DAO recordset handling, based on the JET database engine, is generally considered obsolete -- having been supplanted first by ODBC (MFC CDatabase/CRecordset), then ADO (ActiveX), then OLE DB -- that's why I didn't mention it. However, I can't think of any reason not to use it if it suits your purposes.
There are three forms of the CDaoRecordset::Open() fn and you ar using the first one in which the first paramerte is an integer nOpenTYpe and the value AFX_DAO_USE_DEFAULT_TYPE lets the system choose automatically between dbOpenDynaset, dbOpenTable, and dbOpenSnapshot (my read indicates that AFX_DAO_USE_DEFAULT_TYPE will equate with dbOpenDynaset).
The other two forms expect the first parameter to be a pointer to a CDaoTableDef object and I have no experience working with that object. It is covered here:
DAO Tabledef: Using Tabledefs
http://msdn.microsoft.com/library/en-us/vccore/html/_core_DAO_Tabledef.3a_.Using_Tabledefs.asp
Note that it is possible to insert data, delete records, etc. without bothering with recordsets by using the CDaoDatabase::Execute() function. Just compose a valid SQL statement and pass it in. For instance,
sSQL= "INSERT INTO MyTable (lastName,firstName,city) VALUES ( 'Jones', 'Tim', 'Tulsa' )";
or
sSQL= "DELETE FROM MyTable WHERE lastName= 'Jones' ";
db.Execute( sSQL, dbSQLPassThrough );
There are three forms of the CDaoRecordset::Open() fn and you ar using the first one in which the first paramerte is an integer nOpenTYpe and the value AFX_DAO_USE_DEFAULT_TYPE lets the system choose automatically between dbOpenDynaset, dbOpenTable, and dbOpenSnapshot (my read indicates that AFX_DAO_USE_DEFAULT_TYPE will equate with dbOpenDynaset).
The other two forms expect the first parameter to be a pointer to a CDaoTableDef object and I have no experience working with that object. It is covered here:
DAO Tabledef: Using Tabledefs
http://msdn.microsoft.com/library/en-us/vccore/html/_core_DAO_Tabledef.3a_.Using_Tabledefs.asp
Note that it is possible to insert data, delete records, etc. without bothering with recordsets by using the CDaoDatabase::Execute() function. Just compose a valid SQL statement and pass it in. For instance,
sSQL= "INSERT INTO MyTable (lastName,firstName,city) VALUES ( 'Jones', 'Tim', 'Tulsa' )";
or
sSQL= "DELETE FROM MyTable WHERE lastName= 'Jones' ";
db.Execute( sSQL, dbSQLPassThrough );
ASKER
Dan,
I had another question and figured it deserved its own question (don't want to milk these 500 points)... if you could comment I'd appreciate it.
https://www.experts-exchange.com/questions/21813246/What-db's-can-I-claim-support-for.html
I had another question and figured it deserved its own question (don't want to milk these 500 points)... if you could comment I'd appreciate it.
https://www.experts-exchange.com/questions/21813246/What-db's-can-I-claim-support-for.html
If you have a DSN (as set up in the ODBC control panel) then it is very easy to work with databases using MFC. Let me know if that is the case and I'll cover it.
If you do not have a DSN, then your best bet is probably to use ADO database access. A very simple example of that is shown here:
How to connect to sql server 2K?
https://www.experts-exchange.com/questions/20534401/How-to-connect-to-sql-server-2K.html
Here is a project in which a guy wrapped that all up into an MFC object:
ARSdb - Abstracted Database and Recordset Classes
http://codeguru.earthweb.com/mfc_database/arsdb.shtml
You can also create an ATL data access object (which uses OLE DB), but that can be kind of ticky, what with all of the ATL stuff.
-- Dan