Solved

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

Posted on 2009-07-15
2
233 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XAML: Layout 8 25
c# LinkButton OnClientClick 2 34
WKHTMLTOPDF - --disable-smart-shrinking not working 10 37
Connection String 16 43
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now