我想试用存储过程看看
但遇到了一个问题,以下是错误提示:
-----------------------------------------------------------------------------------------必须声明变量 '@ID'。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 必须声明变量 '@ID'。源错误:
行 43: cmd.Parameters.Add("@ID", TextBox1.Text)
行 44: conn.Open()
行 45: DataGrid1.DataSource = cmd.ExecuteReader
行 46: DataGrid1.DataBind()
行 47: conn.Close()
源文件: C:\Inetpub\wwwroot\myfirstpage\ceshi.aspx.vb 行: 45 堆栈跟踪:
[SqlException: 必须声明变量 '@ID'。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteReader()
myfirstpage.ceshi.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\myfirstpage\ceshi.aspx.vb:45
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()我的源文件和存储过程如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dsn As String = ConfigurationSettings.AppSettings("con")
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString = dsn
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "proc1"
cmd.Parameters.Add("@ID", TextBox1.Text)
conn.Open()
DataGrid1.DataSource = cmd.ExecuteReader
DataGrid1.DataBind()
conn.Close()
End Sub
存储过程:
CREATE PROCEDURE proc1
(@ID char(10))
as
declare @SQLSTR char(1000)
set @SQLSTR= "select * from login where l_id=@ID"
exec(@SQLSTR)
GO到底哪错了 ????
但遇到了一个问题,以下是错误提示:
-----------------------------------------------------------------------------------------必须声明变量 '@ID'。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 必须声明变量 '@ID'。源错误:
行 43: cmd.Parameters.Add("@ID", TextBox1.Text)
行 44: conn.Open()
行 45: DataGrid1.DataSource = cmd.ExecuteReader
行 46: DataGrid1.DataBind()
行 47: conn.Close()
源文件: C:\Inetpub\wwwroot\myfirstpage\ceshi.aspx.vb 行: 45 堆栈跟踪:
[SqlException: 必须声明变量 '@ID'。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteReader()
myfirstpage.ceshi.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\myfirstpage\ceshi.aspx.vb:45
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()我的源文件和存储过程如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dsn As String = ConfigurationSettings.AppSettings("con")
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString = dsn
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "proc1"
cmd.Parameters.Add("@ID", TextBox1.Text)
conn.Open()
DataGrid1.DataSource = cmd.ExecuteReader
DataGrid1.DataBind()
conn.Close()
End Sub
存储过程:
CREATE PROCEDURE proc1
(@ID char(10))
as
declare @SQLSTR char(1000)
set @SQLSTR= "select * from login where l_id=@ID"
exec(@SQLSTR)
GO到底哪错了 ????
cmd.Parameters["@ID"].Value = Convert.ToInt32(TextBox1.Text)
上面我声名的@ID是int类型的。你的是string的
cmd.Parameters.Add("@ID",SqlDbType.类型要换一下);试试将
cmd.CommandText = "proc1"
cmd.Parameters.Add("@ID", TextBox1.Text)
换成
cmd.CommandType = CommandType.Text
cmd.CommandText = "proc1 " + TextBox1.Text