Create an SQL database from a cmd prompt?

G

Guest

I know with SQL you can create a datatable with an SQL string, but is there anyway of creating an entire database using an sql string and some sort of command line program? (Command line; see: program i can run, pass an SQL string into and execute without any hassle)
If it is not possible with SQL is there any other technology that can do this?

Hope someone knows....

jax
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

As far as I know, you can use the isql.exe tool to run any SQL scripts from
the command line.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jax said:
I know with SQL you can create a datatable with an SQL string, but is
there anyway of creating an entire database using an sql string and some
sort of command line program? (Command line; see: program i can run, pass an
SQL string into and execute without any hassle)
 
R

Roman S. Golubin

Hi, Jax
I know with SQL you can create a datatable with an SQL string, but is
there anyway of creating an entire database using an sql string and some
sort of command line program? (Command line; see: program i can run, pass an
SQL string into and execute without any hassle)
If it is not possible with SQL is there any other technology that can do
this?

isql ?
 
L

Lance

You can use isql to read an input file that you have SQL commands in, and if
you have the right commands, build a adatabase, update tables or whatever.

just try typing 'isql' on the command line to pull it up.


Jax said:
I know with SQL you can create a datatable with an SQL string, but is
there anyway of creating an entire database using an sql string and some
sort of command line program? (Command line; see: program i can run, pass an
SQL string into and execute without any hassle)
 
U

UAError

When you say SQL database do you mean "MS SQL Server" database?

If yes, the following applies:

isql.exe still works but osql.exe is the up-to-date version

For an example script of how to create a database
look at the northwind installation script "instnwnd.sql" in

C:\Program Files\Microsoft SQL Server\MSSQL\Install\instnwnd.sql

if "C:\Program Files\Microsoft SQL Server" is your SQL installation directory

Install the SQL Server Books Online as a reference, if you haven't already

SQL Server 2000 Books Online (Updated - SP3)
http://www.microsoft.com/downloads/...a6-bcf4-45a6-a2e2-f6ab5be3ef12&displaylang=en

Microsoft SQL Server 2000 Desktop Engine (MSDE 2000) Release A
http://www.microsoft.com/downloads/...D1-A0BC-479F-BAFA-E4B278EB9147&displaylang=en



Other than that, the process of database creation is highly dependent on your database vendor,
its even more varied than their various SQL dialects.
 
U

UAError

And to make the post the a bit more on topic:
You may as well throw the code into an Installation Component

NET Framework Class Library: Installer Class
http://msdn.microsoft.com/library/d...emconfigurationinstallinstallerclasstopic.asp

---------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;

namespace InstallNorthwind {

// i.e .invoke this class during execution
// of the assembly
[RunInstaller(true)]
public class InstallNorthwind : System.Configuration.Install.Installer {
private System.ComponentModel.Container components = null;

public InstallNorthwind() {
InitializeComponent();
}

protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Component Designer generated code
private void InitializeComponent() {
components = new System.ComponentModel.Container();
}
#endregion

public override void Install(
System.Collections.IDictionary savedState
){
// call the Install() method of the base class
base.Install( savedState );
string strSqlFilePath = this.Context.Parameters["Args"];

// Run the osql process to run the database script
ProcessStartInfo psi =
new ProcessStartInfo(
"osql.exe ",
"-E -i " + "\"" + strSqlFilePath + "\""
);

psi.WindowStyle = ProcessWindowStyle.Hidden;

try {
Process p = Process.Start( psi );
p.WaitForExit();
} catch (Exception e){
// throw an install exception with
// the original exception message
throw new InstallException( e.Message );
}
}

public override void Commit(
System.Collections.IDictionary savedState
){
base.Commit( savedState );
}

public override void Rollback(
System.Collections.IDictionary savedState
){
base.Rollback( savedState );
}

public override void Uninstall(
System.Collections.IDictionary savedState
){
base.Uninstall( savedState );
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top