FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour ADO -ms-sql - xharb 1.0 - create database user schema?
Posts: 204
Joined: Mon Oct 17, 2005 09:09 PM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Mon Feb 25, 2008 04:27 PM

I have to create a database in MS-Sql server automatically from my install program.

Is it possible, using ADO, to issue direct native "sql-string" calls to a sql-server to create the following?
- users
- databases
-schemas?

Don Lowenstein
www.laapc.com
Posts: 711
Joined: Thu Oct 06, 2005 09:57 PM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Mon Feb 25, 2008 05:15 PM

I am doing with Oracle + ADO (users + schemas)

Un saludo



Manuel
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Tue Feb 26, 2008 01:23 AM

Don

Generally I have to get our DBA's involved to set up a database for me and set up a userid for my connection.

Once that is done .. I control the application ( users ) thru tables .. Hopefully you will be given 'create' and 'drop' rights ..

I have my own 2005 SQL server that I use as my development box .. and I control that and do everything I need with SQL Studio 2005.

Hope that helps .. I don't think you can create a database in a schima with scripts...

Rick

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Tue Feb 26, 2008 06:27 AM

Once a database is created, we can create tables, indexes, even procedures through ADO. Both on Oracle and MSSQL.

One way is by sending the create scripts as commands in execute method and another way is to use ADOX.

In any case the user logged in should have create permissions.

I do not know if we can create database itself though ADO.

Regards



G. N. Rao.

Hyderabad, India
Posts: 60
Joined: Fri Oct 07, 2005 05:38 AM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Tue Feb 26, 2008 09:38 AM
It works for me on MS SQL server 2005
#INCLUDE "FIVEWIN.CH"
#INCLUDE "xbrowse.CH"

//----------------------
Function main()

   LOCAL oRs1,oRs2,oRs3,oErr,cSQl1,cSql2, cSQL,cTitle,oBrw,oWnd
	msgInfo("Start")
	cSQL :=  "create database jbc"
	cSql1:=	"CREATE LOGIN jbc2  WITH PASSWORD = 'hhhh'; ";
				+"USE jbc; ";
				+"CREATE USER jbc2 FOR LOGIN jbc2 "
	cSQl2	:=	"CREATE SCHEMA jbc AUTHORIZATION jbc2 ";
    			+"CREATE TABLE test (source int, cost int, partnumber int) ";
    			+"GRANT SELECT TO jbc2 "
   oRs1 := TOleAuto():New( "ADODB.Recordset" )
   oRs1:CursorType := 1                          // opendkeyset
   oRs1:CursorLocation := 3                      // local cache
   oRs1:LockType := 3                            // lockoportunistic
   TRY
   	oRS1:Open( cSql,'Provider=SQLOLEDB;Data Source=OGNEW;Initial Catalog=;User Id=sa1;Password=qqqe' )
   	CATCH oErr
   	MsgInfo( "Error in creating database" )
   	RETURN(.F.)
	END TRY
   oRs3 := TOleAuto():New( "ADODB.Recordset" )
   oRs3:CursorType := 1                          // opendkeyset
   oRs3:CursorLocation := 3                      // local cache
   oRs3:LockType := 3                            // lockoportunistic
   TRY
   	oRs3:Open( cSql1,'Provider=SQLOLEDB;Data Source=OGNEW;Initial Catalog=;User Id=sa1;Password=qqqe' )
   	CATCH oErr
   	MsgInfo( "Error in creating user" )
   	RETURN(.F.)
	END TRY
	
   oRs2 := TOleAuto():New( "ADODB.Recordset" )
   oRs2:CursorType := 1                          // opendkeyset
   oRs2:CursorLocation := 3                      // local cache
   oRs2:LockType := 3                            // lockoportunistic
   
   TRY
   	oRs2:Open( cSql2,'Provider=SQLOLEDB;Data Source=OGNEW;Initial Catalog=jbc;User Id=sa1;Password=qqqe' )
   	CATCH oErr
   	MsgInfo( "Error in crating  table" )
   	RETURN(.F.)
	END TRY
Return nil


That scripts generate database, user and schema

regards Eugeniusz
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Tue Feb 26, 2008 01:07 PM

Mr Eugeniusz Owsiak

Thanks for the info.

Regards



G. N. Rao.

Hyderabad, India
Posts: 204
Joined: Mon Oct 17, 2005 09:09 PM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Tue Feb 26, 2008 02:06 PM

I will try the test for creating databases provided by Mr Owsiak. This may be just what I was looking for.

I would like to deliver our applications to clients with complete seamlessness for end users. Many of our end users are not very computer savy.

MS-Sql server is becoming quite popular and dependable and many small businesses (banks in my case). These clients are leary of .dbf files and want the MS-Sql database. Most of these clients don't have a DBA.

This is very useful for me - I want to deliver a solution as seamless as when I did it with .dbf files. I would package the sql-server engine with our distribution CD and build/maintain the databases 100% within code.

Most likely, I would be the only one ever using the Visual Studio / Enterprise Manager for actual administration of the end databases. Most of my users want to be insulated from these tasks.

Thanks again to all who replied.

Don Lowenstein
www.laapc.com
Posts: 17
Joined: Thu Feb 28, 2008 06:56 PM

Thank you for your Help

Posted: Fri Feb 29, 2008 03:33 PM

Eugeniusz,

I wanted to take a moment to thank you for your examples of creating a MS-SQL database, login, user, and schema. Your examples were very helpful, and your response is greatly appreciated.

I did modularize the calls to allow calling each create individually. Also for flexibility, we capture the varaibles(sa password, database name, login...) via a dialog.

One other modification I made is adding an execute to change the login/user to a "db_owner" as follows:

MDB is the DataBase name from the dialog
MLOGIN is the MS-SQL Login from the dialog
cUSER is the DataBase User from the dialog

LOCAL cSQLCdbu := "USE " + MDB + "; " ;
+"CREATE USER "+cUSER+" FOR LOGIN "+MLOGIN+ "; " ;
+"EXEC sp_addrolemember 'db_owner', '" +cUSER+"'"

We certainly appreciate your assistance.

Regards,
Perry Nichols

Posts: 60
Joined: Fri Oct 07, 2005 05:38 AM

ADO -ms-sql - xharb 1.0 - create database user schema?

Posted: Sat Mar 01, 2008 11:31 AM
Mr Lowenstain
You can also from management studio
1. make setup of database, users, logins schemas etc
2. creating backap to file,
3. copying backap file to your didrtibution media
4.restoring database with this script:
#INCLUDE "FIVEWIN.CH"

//----------------------
Function main()

   LOCAL oRs1,oErr,cSQL
	msgInfo("Start")

	cSQL:="RESTORE DATABASE jbc ";
   	+"FROM DISK = 'c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\jbc.bak' "

   oRs1 := TOleAuto():New( "ADODB.Recordset" )
   oRs1:CursorType := 1                          // opendkeyset
   oRs1:CursorLocation := 3                      // local cache
   oRs1:LockType := 3                            // lockoportunistic
   TRY
   	oRS1:Open( cSql,'Provider=SQLOLEDB;Data Source=OGNEW;Initial Catalog=;User Id=sa1;Password='qqqe' )
   	CATCH oErr
   	MsgInfo( "Error in restoring database" )
	END TRY
Return nil

This is easiest way to solve your problem

regards Eugeniusz

Continue the discussion