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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

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
digital_thoughtsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.