Solved

How do I run a sql script(text file) from a c# windows application using SQLDMO

Posted on 2009-07-15
2
234 Views
Last Modified: 2012-05-07
I am trying to create a 'Database Installer', that
a) Creates a database
b) runs a script from a text file

I used the code (in the code section)




I want to know how to run script from my text file(having the sql script)

// Assign a name to database
				nDatabase.Name = strDatabaseName;
				// Assign a name to datafile
				nDBFileData.Name = strDatabaseName;
				nDBFileData.PhysicalName = srv.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Data.mdf";
				nDBFileData.PrimaryFile = true;
				nDBFileData.Size = 2;
 
				nDBFileData.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB;
				nDBFileData.FileGrowth = 1;
 
				//Add the DBFile object
				nDatabase.FileGroups.Item("PRIMARY").DBFiles.Add(nDBFileData);
 
				// Assign name to Log files
				nLogFile.Name = strDatabaseName + "Log";
				nLogFile.PhysicalName = srv.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Log.ldf";
				nLogFile.Size = 2;
				nDatabase.TransactionLog.LogFiles.Add(nLogFile);

Open in new window

0
Comment
Question by:vsoftsol
2 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 250 total points
ID: 24865661
Hi!

For that propose I usually use the SQLCMD tool:
http://msdn.microsoft.com/en-us/library/ms162773.aspx

Use the -i parameter to pass the input file path you want to execute.

If you don't know how to demand the execution of an exe from withing a C# application you can use the attached code.

Also add the SQLCMD.exe file to the same folder as your application.
System.Diagnostics.Process.Start("SQLCMD.exe", @"-U sa -P P@ssw0rd -S MyServer -i c:\myscript.sql");

Open in new window

0
 

Author Comment

by:vsoftsol
ID: 24877810
Thank you.Worked.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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