Link to home
Start Free TrialLog in
Avatar of Thomas Stockbruegger
Thomas StockbrueggerFlag for Germany

asked on

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

Avatar of digital_thoughts
digital_thoughts

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.
Avatar of Thomas Stockbruegger

ASKER

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
Avatar of Scott Pletcher
"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 :-) ).
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
 
ASKER CERTIFIED SOLUTION
Avatar of digital_thoughts
digital_thoughts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

...same error only now the datename is displayed like this:''C:\SQL_Data\partsdat.mdf '
sorry....sorry....it works.....this was right, thank you.
 
thank you