Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone
have a good time

i know that how can i connect to a "mdf" file in visual c#, but i can't connect to that with visual c++.
can anyone help me?
i used "CDatabase" class but didn't get result.
thanks a lot
Posted
Updated 21-Dec-12 1:05am
v2
Comments
Richard MacCutchan 21-Dec-12 7:40am    
If you want some help then you need to provide more details about your problem. Do not expect people to guess what you mean by expressions such as "but didn't get result.".
Jochen Arndt 21-Dec-12 9:06am    
Some general tips:

If you want to answer to a comment, use the 'Reply' link on the right side of that comment (like I have done here). Then the poster of the first comment gets an email notification and might answer. Otherwise, he will see your post only when reviewing your question what probably would not happen with a two day old question.

You should not post code blocks in comments. It's really hard to read. If the code clarifies your question (like in this case), add it to your question using the green 'Improve question' link (don't forget to format it well there by marking it as code section). The side effect of editing is that your question will be pushed back on top of the questions.

If you edit your question don't just add the code block. Add also some text where you have problems and what you have tried (e.g. error messages, tried tracing with the debugger and saw that this variable does not contain the expected result or so).

All these tips will increase the chance to get an answer.

1 solution

All right my friend
this is code that i used:

C++
CDatabase database;
	CString SqlString;
	CString ssn,firstname;
	CString sDriver = _T("SQL Server");
	CString sDsn, sMc;
    sMc.Format(_T("."));//MERILDSK33
	CString sFile = _T("G:\\shop.mdf");
	sDsn.Format(_T("ODBC;DRIVER={%s};Server=%s;Database=%s;Trusted_Connection=yes"),sDriver,sMc,sFile);

	// You must change above path if it's different
	int iRec = 0; 	
	try
	{
		// Open the database
		database.Open(NULL,false,false,sDsn);
		// Allocate the recordset
		CRecordset recset(&database);
		// Build the SQL statement
		SqlString =  _T("select * from dbo.items");
		// Execute the query
		recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
		// Reset List control if there is any data
		while( !recset.IsEOF() )
		{
			recset.GetFieldValue(_T("SSN"),ssn);
			recset.GetFieldValue(_T("PersonFN"),firstname);
			recset.MoveNext();
		}
		database.Close();
	}
	catch(CDBException* e)
	{
		AfxMessageBox(e->m_strError);
	}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900