Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/MFC
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 18-Dec-12 3:39am
(unknown member)
Edited 21-Dec-12 1:05am
v2
Comments
Richard MacCutchan at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

All right my friend
this is code that i used:
 
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);
	}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100