How create a Database.....USE Master GO CREATE DATABASE ..... please help

I would like to create a new database using my Sql code in c++.
At the code snippet you see how I have created a new table in a existing database called OMSDATEN.
I would like to create a new database like this one with my sql.Format string.
How do I have to change my code that this will work.
Please let me know, thanks.
500 points with grade A on a solution.
Best regards,
Thomas


sql.Format("USE Master GO CREATE DATABASE Parts\
               ON (NAME = parts_dat, FILENAME ='C:\SQL_Data\partsdat.mdf',\
               SIZE =70MB, MAXSIZE=500MB, FILEGROWTH=10MB)\
               LOG ON (NAME=parts_log, FILNAME='C:\SQL_Data\partsdat.log',\
               SIZE=25MB, MAXSIZE=150MB, FILEGROWTH=5MB) GO");

void COffenePostenDialog_Detail::OnBnClickedButton3()
{
     //SQL Server
	m_strConnection = _T("Driver={SQL Server}; Server=DSERVER; Database=OMSDATEN;Uid=;Pwd;");
	 
	//Initialize the Recordset and binding pointers
	m_ptrRs = NULL;   
 
      m_piAdoRecordBinding = NULL;   
 
	//Initialize the COM environment
	::CoInitialize(NULL);
	try
	{
		//Create the recordset object
		m_ptrRs.CreateInstance(__uuidof(Recordset));  
    
 
	 
 
	    CString sql;
 
 
 
//=================================================================
 	sql.Format("CREATE TABLE customer(cust_id int not null,\
			fname varchar(30) not null,\
                  lname varchar(30) not null,\
                  addr1 varchar(40) null,\
			addr2 varchar(40)null,\
			city varchar(40) null,\
			zip char(9) null)\
			INSERT customer\
			cust_id,fname,lname,addr1,city,zip)\
		      VALUES(1,'Thomas','Stockbruegger','2262 SE Ocean Blvd','Stuart','33496')\
			INSERT customer\
			(cust_id,fname,lname)\
			 VALUES(2,'Hans','Meier')\
			 INSERT customer\
			 (cust_id,fname,lname,addr1,addr2,city,zip)\
			  VALUES(3,'John','McCoy','2240 Ocean Blvd','Apt#140','Los Angeles','94550')");
//=================================================================
_bstr_t bstrQuery(sql);
  
		//Open the recordset object Tabelle 
 m_ptrRs->Open(_variant_t(bstrQuery),(LPCTSTR)m_strConnection, adOpenDynamic, adLockOptimistic, adCmdUnknown);
 
 
		//Get a pointer to the record-binding interface Coils
		if(FAILED(m_ptrRs->QueryInterface(__uuidof(IADORecordBinding),(LPVOID *)&m_piAdoRecordBinding)))     _com_issue_error(E_NOINTERFACE);
  
		//Bind the record class to the recordset
	//	m_piAdoRecordBinding->BindToRecordset(&m_rsRecSet);  
	
	
 
	 
}
 
//###########################################################################################
 
		//---------- Any erros? -------------
    	catch (_com_error &e)
		{
			//Display the error
			GenerateError(e.Error(), e.Description());
		}
		//-----------------------------------
	 //Close the recordset
//	if(m_ptrRs)  m_ptrRs->Close();    waren
	
 
	// Do we have a valid pointer to the record binding?
	if(m_piAdoRecordBinding) m_piAdoRecordBinding->Release();      
	
	//Set the recordset pointer to NULL
	m_ptrRs = NULL;    
	
	//Shut down the COM environment
	::CoUninitialize();
 
 
}

Open in new window

Thomas StockbrueggerAsked:
Who is Participating?
 
digital_thoughtsConnect With a Mentor Commented:
I think its because \ is a "special" character in C(++)(#) languages, so you need either an escape key, or just two of the slashes:

sql.Format("CREATE DATABASE Parts\
               ON (NAME = parts_dat, FILENAME ='C:\\SQL_Data\\partsdat.mdf',\
               SIZE =70MB, MAXSIZE=500MB, FILEGROWTH=10MB)\
               LOG ON (NAME=parts_log, FILENAME='C:\\SQL_Data\\partsdat.log',\
               SIZE=25MB, MAXSIZE=150MB, FILEGROWTH=5MB)");
0
 
digital_thoughtsCommented:
Are you getting an error with you try to use the:

sql.Format("USE Master GO CREATE DATABASE Parts\
               ON (NAME = parts_dat, FILENAME ='C:\SQL_Data\partsdat.mdf',\
               SIZE =70MB, MAXSIZE=500MB, FILEGROWTH=10MB)\
               LOG ON (NAME=parts_log, FILNAME='C:\SQL_Data\partsdat.log',\
               SIZE=25MB, MAXSIZE=150MB, FILEGROWTH=5MB) GO");

command?
One thing I did notice is you have a typo in the statment, you have FILNAME instead of FILENAME, maybe that's your problem... otherwise as log as the C:\SQL_Data directory exists, the SQL statement should work.
0
 
Thomas StockbrueggerAuthor Commented:
thanks for your answer. Yes I forgot the E on FILENAME.
But that was not the error.
I got the error message    wrong syntax in row 1 near GO
I guess I have to change this, because I will now build a new database.
Database=OMSDATEN is a database that allready exist.
m_strConnection = _T("Driver={SQL Server}; Server=DSERVER; Database=OMSDATEN;Uid=;Pwd;");
 
 Please help....thanks
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Scott PletcherSenior DBACommented:
"GO" is not actually SQL, it's a T-SQL / batch processor instruction.  You don't have to be in master to create a db anyway, nor do you have to have a GO at the end.  So just reduce the code to:

sql.Format("CREATE DATABASE Parts\
               ON (NAME = parts_dat, FILENAME ='C:\SQL_Data\partsdat.mdf',\
               SIZE =70MB, MAXSIZE=500MB, FILEGROWTH=10MB)\
               LOG ON (NAME=parts_log, FILENAME='C:\SQL_Data\partsdat.log',\
               SIZE=25MB, MAXSIZE=150MB, FILEGROWTH=5MB)");

I think you'll be OK then (assuming the rest of the syntax is correct :-) ).
0
 
Thomas StockbrueggerAuthor Commented:
Your are right about the GO. But now I get a new error.
 
Medienactivationerror. The phy.datename 'C:SQL_Datapartsdat.mdf could be wrong.
 
I have created a folder with SQL_Data on my C:drive.
It is also strange the the datename is displayed like this:'C:SQL_Datapartsdat.mdf'
and not like 'C:\SQL_Data\partsdat.mdf '
I guess something is wrong with my syntax. Please help. Thanks a lot.
Best regards,
Thomas
 
0
 
Thomas StockbrueggerAuthor Commented:

...same error only now the datename is displayed like this:''C:\SQL_Data\partsdat.mdf '
0
 
Thomas StockbrueggerAuthor Commented:
sorry....sorry....it works.....this was right, thank you.
 
0
 
Thomas StockbrueggerAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.