473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3027

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 CreateStoredProcedures()
Dim alSql As New ArrayList
alSql.Add("CREATE PROC usp_ProjectResultsByID(inID VARCHAR(50)) AS SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection(connectionString)
dbCommand = New OleDbCommand
End If
dbCommand.Connection = dbConnection
dbConnection.Open()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.CommandText = DirectCast(alSql(i), String)
dbCommand.ExecuteNonQuery()
Next
dbConnection.Close()
End Sub

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

' Create the SelectCommand.
cmd = New OleDbCommand("SELECT * FROM Customers " & "WHERE Country =
@Country AND City = @City", conn)
cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)
cmd.Parameters.Add("@City", OleDbType.VarChar, 15)
da.SelectCommand = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("INSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40,
"CompanyName")
da.InsertCommand = cmd

--
Dennis in Houston
"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

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****@discussions.microsoft.com> schreef in bericht
news:55**********************************@microsof t.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 CreateStoredProcedures()
Dim alSql As New ArrayList
alSql.Add("CREATE PROC usp_ProjectResultsByID(inID VARCHAR(50)) AS
SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection(connectionString)
dbCommand = New OleDbCommand
End If
dbCommand.Connection = dbConnection
dbConnection.Open()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.CommandText = DirectCast(alSql(i), String)
dbCommand.ExecuteNonQuery()
Next
dbConnection.Close()
End Sub

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

' Create the SelectCommand.
cmd = New OleDbCommand("SELECT * FROM Customers " & "WHERE Country
=
@Country AND City = @City", conn)
cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)
cmd.Parameters.Add("@City", OleDbType.VarChar, 15)
da.SelectCommand = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("INSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40,
"CompanyName")
da.InsertCommand = cmd

--
Dennis in Houston
"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

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

"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

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****@discussions.microsoft.com> schreef in bericht
news:55**********************************@microsof t.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 CreateStoredProcedures()
Dim alSql As New ArrayList
alSql.Add("CREATE PROC usp_ProjectResultsByID(inID VARCHAR(50)) AS
SELECT
* FROM _ qryResults WHERE ID = inID")
If dbConnection Is Nothing Then
dbConnection = New OleDbConnection(connectionString)
dbCommand = New OleDbCommand
End If
dbCommand.Connection = dbConnection
dbConnection.Open()
Dim i As Integer
For i = 0 To alSql.Count - 1
dbCommand.CommandText = DirectCast(alSql(i), String)
dbCommand.ExecuteNonQuery()
Next
dbConnection.Close()
End Sub

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

' Create the SelectCommand.
cmd = New OleDbCommand("SELECT * FROM Customers " & "WHERE Country
=
@Country AND City = @City", conn)
cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)
cmd.Parameters.Add("@City", OleDbType.VarChar, 15)
da.SelectCommand = cmd

' Create the InsertCommand.
cmd = New OleDbCommand("INSERT INTO Customers (CustomerID,
CompanyName) " & "VALUES (@CustomerID, @CompanyName)", conn)
cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40,
"CompanyName")
da.InsertCommand = cmd

--
Dennis in Houston
"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


Jun 24 '06 #6

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

Similar topics

9
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...
4
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...
10
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...
1
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...
10
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...
1
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...
2
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
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...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.