[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-20
8
Medium Priority
?
758 Views
Last Modified: 2013-11-20
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

0
Comment
Question by:tsp2002
  • 5
  • 2
8 Comments
 
LVL 8

Expert Comment

by:digital_thoughts
ID: 23004528
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
 

Author Comment

by:tsp2002
ID: 23011065
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
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 23017336
"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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:tsp2002
ID: 23027078
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
 
LVL 8

Accepted Solution

by:
digital_thoughts earned 2000 total points
ID: 23033966
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
 

Author Comment

by:tsp2002
ID: 23040359

...same error only now the datename is displayed like this:''C:\SQL_Data\partsdat.mdf '
0
 

Author Comment

by:tsp2002
ID: 23040375
sorry....sorry....it works.....this was right, thank you.
 
0
 

Author Closing Comment

by:tsp2002
ID: 31518631
thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question