PC Review


Reply
Thread Tools Rate Thread

Creating Stored Procedure

 
 
Terrance
Guest
Posts: n/a
 
      10th Jan 2008
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?

--
TC
 
Reply With Quote
 
 
 
 
William Vaughn
Guest
Posts: n/a
 
      10th Jan 2008
It's easier than that. I would use SQLCMD to execute the SQL (assuming it's
properly formatted). Next, I might try SQLDMO but I've written my own SQLCMD
class a couple of times so I would just use that. The code is included on
the DVD in my book.

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
"Terrance" <(E-Mail Removed)> wrote in message
news:1A715BAF-1B82-4595-A9B1-(E-Mail Removed)...
>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?
>
> --
> TC


 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      11th Jan 2008
Open the file in SQL Server Management studio and hit F5. No need to run it
in C#, unless you are going to build the sql bits as part of an install.
Even then, you can tag the command line and run the text file as a batch.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"Terrance" <(E-Mail Removed)> wrote in message
news:1A715BAF-1B82-4595-A9B1-(E-Mail Removed)...
>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?
>
> --
> TC



 
Reply With Quote
 
Chris Anderson [MVP-VB]
Guest
Posts: n/a
 
      12th Jan 2008
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?
>

 
Reply With Quote
 
Shawn Wildermuth
Guest
Posts: n/a
 
      12th Jan 2008
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?
>>



 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      13th Jan 2008
Terrance,

I see a lot of magic here, you get many answers based on a created " Create"
*.sql file, while my thought was that it was about a document text file with
some SQL scripts described in it.

As it is the first, I would not do it, just execute it in SQL management
studio and you are ready.

:-)

Cor

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help creating a stored procedure to call from .Net RSH Microsoft VB .NET 1 16th Aug 2005 06:26 PM
Creating an SQLDB search using a stored procedure Matthew Curiale Microsoft ASP .NET 1 11th Feb 2005 04:08 PM
Creating stored Procedure in Access VMI Microsoft Access 3 4th Jan 2005 08:54 PM
creating extended stored procedure with Visual C++ 7 (vs 2003) AA Microsoft Dot NET Framework 2 2nd Sep 2004 08:19 AM
Creating a stored procedure (query) in access Tor Inge Rislaa Microsoft Access Queries 3 25th May 2004 12:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 PM.