首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数化查询

参数化查询
EN

Stack Overflow用户
提问于 2017-04-05 20:15:15
回答 1查看 55关注 0票数 0

当我试图运行一个页面时,我一直收到这个错误消息。

参数化的查询'(@Medication nvarchar(14),@Quantity nvarchar(9),@RequestedDate n‘)期望参数'@RequestedDate',而该参数没有提供。

这是我的代码隐藏文件:

代码语言:javascript
复制
Dim conn As SqlConnection
Dim cmd As SqlCommand

'Getting today's date to store into database to let staff know when prescription was ordered
'reference for this code : - https://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx

Dim thisDay As DateTime = DateTime.Today
Session("DateRequested") = (thisDay.ToString())
Dim Medication As String
Dim Quantity As String
Dim RequestedDate As String
Dim Pharmacy As String
Dim PatientNumber As String

Medication = txtDrug1.Text + " " + txtDrug2.Text + " " + txtDrug3.Text + " " + txtDrug4.Text + " " + txtDrug5.Text
Quantity = txtQuant1.Text + " " + txtQuant2.Text + " " + txtQuant3.Text + " " + txtQuant4.Text + " " + txtQuant5.Text

RequestedDate = Session("Date Requested")
PatientNumber = Session("PatientNumber")
Pharmacy = txtPharmacy.Text

Dim cmdstring As String = "INSERT INTO Prescription (Medication, QntyandStrength, RequestedDate, Pharmacy, PatientNumber) Values (@Medication, @Quantity, @RequestedDate, @Pharmacy, @PatientNumber)"

conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\Documents\Visual Studio 2015\WebSites\myAppointments\App_Data\Database.mdf;Integrated Security=True")
cmd = New SqlCommand(cmdstring, conn)

cmd.Parameters.AddWithValue("@Medication", Medication)
cmd.Parameters.AddWithValue("@Quantity", Quantity)
cmd.Parameters.AddWithValue("@RequestedDate", RequestedDate)
cmd.Parameters.AddWithValue("@Pharmacy", Pharmacy)
cmd.Parameters.AddWithValue("@PatientNumber", PatientNumber)

conn.Open()

cmd.ExecuteNonQuery()
conn.Close()

MsgBox("Your Prescription Has Been Requested!")

数据库类型为:

代码语言:javascript
复制
[PrescriptionNumber] INT          NOT NULL,
[Medication]         NVARCHAR(250) NULL,
[QntyandStrength]    NVARCHAR(250) NULL,
[RequestedDate]      NVARCHAR(70) NULL,
[Pharmacy]           VARCHAR (50) NULL,
[PatientNumber]      CHAR (10)    NULL,

关于如何改变这个问题有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2017-04-06 14:17:48

正如Chris所指出的,您对会话变量的引用是不正确的。

声明变量并赋值:

代码语言:javascript
复制
Session("DateRequested") = (thisDay.ToString())

...but然后你用不同的名字来指它.

代码语言:javascript
复制
RequestedDate = Session("Date Requested")  ''This variable name has a space.

所以,当你传递参数时.

代码语言:javascript
复制
cmd.Parameters.AddWithValue("@RequestedDate", RequestedDate)

.传递对空对象的引用.没有会议(“请求日期”)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43240819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档