必须声明sclar变量"@CostCenter". [英] Must declare the sclar variable "@CostCenter"

查看:97
本文介绍了必须声明sclar变量"@CostCenter".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是这种编程语言的新手.这是我的代码;我想使用sqlstring操作数据库

hello guys, im new to this programming language. here is my code; i want to manipulate database using sqlstring

<script language="VB" runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim costcenter As String = Session("costcenter")
        lbl1.Text = costcenter
        If lbl1.Text = "12941" Then
            Dim strSQL As String
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Integrated Security=SSPI;" & "Initial Catalog=TestSQL")
            strSQL = "SELECT * FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            GridView1.DataSource = myCommand.ExecuteReader()
            GridView1.DataBind()
            connection.Close()
        End If
    End Sub



我的问题是当我运行它时发生错误
Must declare the scalar variable "@CostCenter".
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.



My question is when i run it an error occured
Must declare the scalar variable "@CostCenter".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@CostCenter".

Source Error:

Line 14:             connection.Open()
Line 15:             Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
Line 16:             GridView1.DataSource = myCommand.ExecuteReader()
Line 17:             GridView1.DataBind()
Line 18:             connection.Close()


Source File: C:\Documents and Settings\oca14625\Desktop\WebSite3l\connectdatabase.aspx    
Line: 16

我需要设置参数,但不知道语法
您能否提供给我一个示例代码..预先感谢.

I need to set parameters but dont know the sytax
could you provide me a sample code.. Thanks in advance.

推荐答案

您可以在ExecuteReader方法之前添加此代码.
You can add this code before the ExecuteReader method.
myCommand.Parameters.AddWithValue("@CostCenter", [value of your cost center])


这篇关于必须声明sclar变量"@CostCenter".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆