473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programatically create a Stored Procedure

Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian

Jun 22 '06 #1
5 3081

da*******@gmail .com wrote:
Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian


I assume you know how to make a SP in, say, the Enterprise Manager?
Just make the proc, and then in EM, run the Generate SQL Scripts
function on the SP, and click on the preview window. It will show you
the transact-sql code you need to create the SP.

Or just review the transact-sql help from the Query Analyzer for the
syntax.

In code, just use a SQLCommand, and set the CommandText to a character
string that contains the entire code to make the SP and execute it as a
non-query.

Jun 22 '06 #2
Here's excerpts that I copied sometime ago from this newsgroup. I don't know
if they work or not but may give you some ideas:

Access DataBase:

Private Sub CreateStoredPro cedures()
Dim alSql As New ArrayList
alSql.Add("CREA TE PROC usp_ProjectResu ltsByID(inID VARCHAR(50)) AS SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection (connectionStri ng)
dbCommand = New OleDbCommand
End If
dbCommand.Conne ction = dbConnection
dbConnection.Op en()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.Comma ndText = DirectCast(alSq l(i), String)
dbCommand.Execu teNonQuery()
Next
dbConnection.Cl ose()
End Sub

SQL DataBase:
**** Storing Commands in DataAdapter for Future Use ***
Dim da As OleDbDataAdapte r = New OleDbDataAdapte r
Dim cmd As OleDbCommand

' Create the SelectCommand.
cmd = New OleDbCommand("S ELECT * FROM Customers " & "WHERE Country =
@Country AND City = @City", conn)
cmd.Parameters. Add("@Country", OleDbType.VarCh ar, 15)
cmd.Parameters. Add("@City", OleDbType.VarCh ar, 15)
da.SelectComman d = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("I NSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters. Add("@CustomerI D", OleDbType.Char, 5, "CustomerID ")
cmd.Parameters. Add("@CompanyNa me", OleDbType.VarCh ar, 40,
"CompanyNam e")
da.InsertComman d = cmd

--
Dennis in Houston
"da*******@gmai l.com" wrote:
Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian

Jun 22 '06 #3
Dennis,

I saw your code, you use dbConnection.

You know that there is now a new class.

http://msdn2.microsoft.com/en-us/library/c790zwhc.aspx

Just to make you attent on it, it does not harm, but while I was reading
your message I was first confused.

Cor

"Dennis" <De****@discuss ions.microsoft. com> schreef in bericht
news:55******** *************** ***********@mic rosoft.com...
Here's excerpts that I copied sometime ago from this newsgroup. I don't
know
if they work or not but may give you some ideas:

Access DataBase:

Private Sub CreateStoredPro cedures()
Dim alSql As New ArrayList
alSql.Add("CREA TE PROC usp_ProjectResu ltsByID(inID VARCHAR(50)) AS
SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection (connectionStri ng)
dbCommand = New OleDbCommand
End If
dbCommand.Conne ction = dbConnection
dbConnection.Op en()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.Comma ndText = DirectCast(alSq l(i), String)
dbCommand.Execu teNonQuery()
Next
dbConnection.Cl ose()
End Sub

SQL DataBase:
**** Storing Commands in DataAdapter for Future Use ***
Dim da As OleDbDataAdapte r = New OleDbDataAdapte r
Dim cmd As OleDbCommand

' Create the SelectCommand.
cmd = New OleDbCommand("S ELECT * FROM Customers " & "WHERE Country
=
@Country AND City = @City", conn)
cmd.Parameters. Add("@Country", OleDbType.VarCh ar, 15)
cmd.Parameters. Add("@City", OleDbType.VarCh ar, 15)
da.SelectComman d = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("I NSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters. Add("@CustomerI D", OleDbType.Char, 5, "CustomerID ")
cmd.Parameters. Add("@CompanyNa me", OleDbType.VarCh ar, 40,
"CompanyNam e")
da.InsertComman d = cmd

--
Dennis in Houston
"da*******@gmai l.com" wrote:
Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian

Jun 23 '06 #4
guy
stored procs?
read this excellent article
http://weblogs.asp.net/fbouma/archiv.../18/38178.aspx

"da*******@gmai l.com" wrote:
Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian

Jun 23 '06 #5
Thanks Cor. I don't have vb2005 yet as I am waiting for SP1 to come out. I
haven't used the code I posted, just copied if from someone's note here on
this newsgroup...I copy intesesting code that people like yourself post and
index it for future use in my master file.
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Dennis,

I saw your code, you use dbConnection.

You know that there is now a new class.

http://msdn2.microsoft.com/en-us/library/c790zwhc.aspx

Just to make you attent on it, it does not harm, but while I was reading
your message I was first confused.

Cor

"Dennis" <De****@discuss ions.microsoft. com> schreef in bericht
news:55******** *************** ***********@mic rosoft.com...
Here's excerpts that I copied sometime ago from this newsgroup. I don't
know
if they work or not but may give you some ideas:

Access DataBase:

Private Sub CreateStoredPro cedures()
Dim alSql As New ArrayList
alSql.Add("CREA TE PROC usp_ProjectResu ltsByID(inID VARCHAR(50)) AS
SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection (connectionStri ng)
dbCommand = New OleDbCommand
End If
dbCommand.Conne ction = dbConnection
dbConnection.Op en()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.Comma ndText = DirectCast(alSq l(i), String)
dbCommand.Execu teNonQuery()
Next
dbConnection.Cl ose()
End Sub

SQL DataBase:
**** Storing Commands in DataAdapter for Future Use ***
Dim da As OleDbDataAdapte r = New OleDbDataAdapte r
Dim cmd As OleDbCommand

' Create the SelectCommand.
cmd = New OleDbCommand("S ELECT * FROM Customers " & "WHERE Country
=
@Country AND City = @City", conn)
cmd.Parameters. Add("@Country", OleDbType.VarCh ar, 15)
cmd.Parameters. Add("@City", OleDbType.VarCh ar, 15)
da.SelectComman d = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("I NSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters. Add("@CustomerI D", OleDbType.Char, 5, "CustomerID ")
cmd.Parameters. Add("@CompanyNa me", OleDbType.VarCh ar, 40,
"CompanyNam e")
da.InsertComman d = cmd

--
Dennis in Houston
"da*******@gmai l.com" wrote:
Alright, so I am creating an entire database in code since I have no
idea how to bundle a created database in an install of my application.
I know how to create the database and the tables in VB.NET code just
fine, what I can't figure out is how to create stored procedures in
VB.NET that will be saved in this same database.

Can anyone help me out on this one?

Darian


Jun 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
11238
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a file to a path, such as F:/myfiledate.txt that holds the text in txtOutput, then the trigger...
4
8103
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure creates a string of custom SQL statement and returns this string back to the main stored procedure. This SQL statements work fine on there own. The SQL returned from the sub stored procedure are returned fine. The datatype of the variable that...
10
2170
by: John A Fotheringham | last post by:
I'm trying to write a procedure that having created a new database, will then create within that new database all the tables and procedures that go with it. In doing this I'm hitting the problem that you can't issue a USE command within a procedure. So my question is either - how do I get around this?
1
1160
by: spiffo | last post by:
Using Sql Srv 7 I know I can use the system sp's sp_stored_procedures and sp_sproc_columns to determine all the sps in a db, and what input parms there are for a particular sp... but... if the sp returns a result set, is there a way to find out the stru of that in a similar manner???
10
1845
by: serge | last post by:
I can not create a stored procedure that calls another not yet created stored procedure? In MS SQL I get a warning that the calling procedure does not exist but the new stored Procedure gets created anyhow. I believe Oracle works similarly. I can not make this possible in DB2?
1
3732
by: Marc Jennings | last post by:
Hi, I need to rebuild a database each time I redeploy a test applicatio, and I was wondering if anyone could give me some clues as to how to go about disconnecting any users that may be logged into that database in SQL. I know I can do this quite easily using MSBuild, but the reason I need to know is so I can get awayt from using beta tools in development. (We have a specific issue with MSBuild) Also, if anyone knows of a good...
2
1727
by: Mukesh | last post by:
Hi all I m Using SQL SERVER 2005 I have a requirement to store some data in xmldatatype using stored procedure , Here is example
6
13055
by: Shiller | last post by:
I want my application to create a new database/tables when run for the first time. I have created a stored procedure to create the new database named "budget". When I run the stored procedure, it creates the budget database but the tables are created in the "master" database. Please help.
11
4105
by: raylopez99 | last post by:
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug (feature?) below. At some point I'm sure I'll be able to laugh about this, akin to forgeting a semi-colon in C/C++, but right now it's frustrating (time to sleep on it for a while). Problem-- For some reason I get the error when trying to save files...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.