This may not be the place to ask this, if not, you can move this post to the SQL forum but I have an application that I'm building which pulls dates from a database. The start value is stored as payPeriodStart date and I add 7 days to get the end date. What I need to know is, is it possible to store those values and pass them to a Stored Procedure? Currently my stored procedures have "static" dates in them ie:
where dateadd (n, Timestamp, '12/31/1899') - ([LoggedIn]/1000)/60/1440+1 Between '10/3/2010' and '10/10/2010'
and I'd like those values to be
Between payPeriodStartD ate and payPeriodEndDat e.
Any help would be greatly appreciated.
Thank you
5 4378
Yes, it is possible to create a stored procedure that takes parameters from a VB app.
Your stored procedure needs to be defined to take the parameters that you need. Also, your code to execute the stored procedure needs to set the parameters.
If you are using SQL Server, the correct syntax to create a stored procedure can be found here. I am not well versed in creating stored procedures though, so that part of this question will be better resolved in the SQL forum.
As far as passing a parameter to a stored procedure in VB, the code looks something like this: -
Dim payPeriodStartDate As Date = Date.Now.Date
-
Dim payPeriodEndDate As Date = payPeriodStartDate.AddDays(14)
-
-
Dim sqlConn As New SqlConnection("")
-
Dim sqlCmd As New SqlCommand("StoredProcedureName")
-
Dim param As SqlParameter
-
-
sqlCmd.Connection = sqlConn
-
sqlCmd.CommandType = CommandType.StoredProcedure
-
-
param = sqlCmd.CreateParameter()
-
param.ParameterName = "@payPeriodStartDate"
-
param.Value = payPeriodStartDate
-
param.SqlDbType = SqlDbType.Date
-
sqlCmd.Parameters.Add(param)
-
-
param = sqlCmd.CreateParameter()
-
param.ParameterName = "@payPeriodEndDate"
-
param.Value = payPeriodEndDate
-
param.SqlDbType = SqlDbType.Date
-
sqlCmd.Parameters.Add(param)
-
-
Dim dataReader As SqlDataReader = Nothing
-
-
Try
-
dataReader = sqlCmd.ExecuteReader()
-
'access your data
-
Catch ex As Exception
-
'whatever your exception handling is
-
End Try
-
Forgive me if this isn't 100% accurate. Its been a while sense I wrote this code in a production environment. I just quickly threw this together. The idea is correct though. Notice that the SqlCommand text is the stored procedure name and the SqlCommand object actually has a property that is set to explicitly state what type of command you are executing.
Joseph,
Thank you for your response. I was looking through my app and discovered that I already had parameters set for the values that I need to pass to my sql stored procedure. My code looks like this: -
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
-
Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
-
"dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
-
"from dbo.payroll" & _
-
" where payrollran = 'no'"
-
Dim oCmd As System.Data.SqlClient.SqlCommand
-
Dim oDr As System.Data.SqlClient.SqlDataReader
-
-
oCmd = New System.Data.SqlClient.SqlCommand
-
Try
-
With oCmd
-
.Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxxx")
-
.Connection.Open()
-
.CommandType = CommandType.Text
-
.CommandText = ssql
-
oDr = .ExecuteReader()
-
End With
-
If oDr.Read Then
-
payPeriodStartDate = oDr.GetDateTime(1)
-
payPeriodEndDate = payPeriodStartDate.AddDays(7)
-
Dim ButtonDialogResult As DialogResult
-
ButtonDialogResult = MessageBox.Show(" The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & " Through End Date: " & payPeriodEndDate.ToString())
-
If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
-
-
exceptionsButton.Enabled = True
-
startpayrollButton.Enabled = False
-
-
End If
-
End If
-
oDr.Close()
-
oCmd.Connection.Close()
-
Catch ex As Exception
-
MessageBox.Show(ex.Message)
-
oCmd.Connection.Close()
-
End Try
-
-
End Sub
-
So I have two questions in regards to this. Should I declare the values outside of this sub? Secondly, since I'm calling the parameters payPeriodStartD ate and payPeriodEndDat e, I'm assuming that in my sql SP, I'll just need to write my query like this:
where dateadd (n, Timestamp, '12/31/1899') - ([LoggedIn]/1000)/60/1440+1 Between payPeriodStartD ate and payPeriodEndDat e
Is that correct?
Thanks
Doug
I'm not sure what you mean by "should I declare the values outside of my sub". Could you elaborate?
Secondly, the a stored procedure is something that is contained completely on the SQL Server. The variable names that you decide to use in your program have no relationship to the parameters of a stored procedure.
joseph,
What I'm asking for is because I'm declaring values that need to be passed on each form, I am thinking that I need to either declare "global" variables or something within a module.
I am aware of the fact that the SP is contained on the SQL server, but my variables (in this case payrollstartdat e, payrollenddate) are pulled from the sql server at the start of this process and I'll need to save them as values to pass to my stored procedures. The way that I have my SP set up is that @payrollstartda te and @payrollenddate replace payrollstartdat e, payrollenddate simply as a cleaner naming convention. I hope that makes sense and I can post my code if necessary for my sp's.
I think I solved this problem. I just created a module:
Module Module1
Public payperiodstartd ate As Date
Public payperiodenddat e As Date
End Module
That seems to have fixed the issue.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike |
last post by:
Hello,
I'm currently working on debugging a very large DTS package that was
created by someone else for the purpose of importing data into my
company's database. The data is mainly user/contact-related data for
our customer base.
We ran into problems when one import, of about 40,000 rows, took
upwards of six hours to complete. Many of the stored procedures used
by this package were written using XML. I've re-written many of them
|
by: Nashat Wanly |
last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and
Visual C# .NET
View products that this article applies to.
This article was previously published under Q310070
For a Microsoft Visual Basic .NET version of this article, see 308049.
For a Microsoft Visual C++ .NET version of this article, see 310071.
For a Microsoft Visual J# .NET version of this article, see 320627.
This article refers to the following Microsoft .NET...
|
by: shyner |
last post by:
Hi Everyone,
I've been battling this for two days with no luck. I'm using SQL
Server 2000.
Here's the mystery: I've got a stored procedure that takes a single
varchar parameter to determine how the result set is sorted. Here it
is:
CREATE PROCEDURE spDemo @SortField varchar(30)
|
by: mhk |
last post by:
Hi,
i have "req_date" column of "datetime" type in Database table besides
other columns.
From my Web page, i am calling the Stored Procedure with variable
parameter "Search_Date" of Varchar(60) type.
the value, i am passing to Stored procedure through "Search_Date" is
compared to req_date column of table.
|
by: erich93063 |
last post by:
I have a stored procedure which is performing a search against a "task"
table. I would like to pass in a variable called @strAssignedTo which
will be a comma delimeted list of employee ID's ie: "5,6,10". So my SQL
without this variable would be something like:
WHERE intAssignedTo IN (5,6,10)
but when I try to do:
WHERE intAssignedTo IN (@strAssignedTo)
| |
by: Dino L. |
last post by:
How can I run stored procedure (MSSQL) ?
|
by: purushneel |
last post by:
Hi,
I work primarily on Oracle databases. I am trying to convert a
recursive stored procedure written in Oracle to DB2.
Does DB2 UDB v8.2 (Windows/AIX) supports recursive stored procedures ??
After some research, I found out that to call recursively in DB2, the
stored procedure should be CALLed using dynamic SQL. I am not sure
whether it is the right way. Am I missing something ?? Please let me
know...
|
by: meenu_susi |
last post by:
i have different colors for shirt...
When I click the blue color the shirt color will change to blue color and
Near to that shirt I will have a button as design now when I click that button it get posted
To next page where I want the changed shirt color to be displayed(as I told when I click blue the shirt will change to blue that blue shirt has to be displayed in next page when I click the button)
Then the script is as follows
...
|
by: chariclark |
last post by:
This may be a quick fix post...
----------------------------
I am having trouble passing multiple values into stored procedure. Here it is below:
CREATE Procedure spGetAssociateds
(
@PDSI int
)
AS
|
by: udaypawar |
last post by:
Hi All,
I have one problem here with mysql stored procedures.
I have a list of ids seperated by comma e.g., (" 'A', 'B', 'C' "). I am passing the same to mysql stored procedure as a parameter. I want to use these values in where clause of select statement using IN.
I can't use prepared statements because queries written in stored procedure are very complex.
|
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...
| |
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,...
|
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...
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
| |
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();...
|
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...
| |