Since you specified using C#, can't you just open a connection and execute
the CREATE PROC call? SMO and such will work too, but it would seem that
something like this (NOTE: Pseudo code):
using (SqlConnection conn = new SqlConnection("..."))
using (SqlCommand cmd = conn.CreateCommand())
{
string storedProc = File.ReadAllText("yourstoredprocfile.txt");
if (storedProc.ToLower().StartsWith("create proc"))
{
cmd.CommandText = storedProc;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
if (conn.State != ConnectionState.Closed) conn.Close();
}
}
}
Thanks,
Shawn Wildermuth
Microsoft MVP (C#)
http://wildermuthconsulting.com
mailto:swildermuth@REMOVE_ALLCAPS_adoguy.com
> Instead of SQLDMO (which is COM based and won't work with SQL2005 (you
> didn't specify the SQL version), give SMO (SQL Management Object) a
> try. In a nutshell, it's the .NET replacement for SQLDMO, and talks to
> SQL2000 and SQL2005 (I don't know about SQL2008).
>
> Once connected, there's a ExecuteWithResults (and an ExecuteNoResults)
> that you can feed the script to. But there's a catch, you can't have
> any GO statements in it. You'll generate an error if you do. What I do
> is split the script up using the GO's, putting each segment into a
> StringCollection (Specialized.StringCollection), which can then be
> passed to the ExecuteWithResults and it'll run them.
>
> Hope this helps.
>
> -ca
>
> Terrance wrote:
>
>> I have a question and I'm not quite sure if this can be done or not.
>> Does any have any idea(s) on how to create a SQL stored procedure
>> from a text file using C#. You see a developer has given me a text
>> file that creates some stored procedures for a database. I am in the
>> process of creating a app that will create the database along with
>> the stored procedures.
>>
>> I don't want to rewrite what the developer wrote I just want to be
>> able to read the file content and run it to create the stored
>> procedures. Will using SQLDMO help?
>>