我使用以下存储过程来呼叫句号以使其与教师匹配。
我们正在编写一个考勤应用程序,从下拉菜单中选择教师的名字,"期间"下拉菜单填充。出于某种原因,我们没有填写期间数据。
存储过程:
ALTER PROCEDURE spGetPeriod @Period varchar(10)
AS
SELECT Period
FROM tmTeacher
WHERE Period = @Period
RETURN
Teacher Table
TeacherId varchar(10) Unchecked
FName varchar(20) Checked
LName varchar(20) Checked
StudentId varchar(10) Unchecked
Period varchar(10) Unchecked
TeacherName varchar(50) Checked
Period Table
PeriodId varchar(10) Unchecked
Period varchar(50) Unchecked
ClassId varchar(10) Unchecked
提前致谢!
调用SP的代码:
'***** SETUP THE CONNECTION ***
SQL = "spGetPeriod '" + (cboTeacher.Text) + "'"
'SQL = "select * from teacher"
Try
With SqlCmd
.CommandText = SQL
'.CommandType = CommandType.StoredProcedure
.Connection = objConnection
'.Parameters.AddWithValue("@TeacherId", cboTeacher.SelectedIndex)
End With
SqlCmd = New SqlCommand(SQL, objConnection)
Catch ex As Exception
Call ErrorControl(ex)
End Try
'*** MAKE THE CONNECTION ***
Try
HandleConnection(objConnection)
'MsgBox("Connection Established!")
ToolStripStatusLabel1.Text = "Connection Established!"
Catch ex As Exception
Call ErrorControl(ex)
End Try
'*** EXECUTE THE COMMAND AND FILL THE COMBO ***
Dim Reader As SqlDataReader = SqlCmd.ExecuteReader()
While Reader.Read()
cboPeriod.Items.Add(Reader(0).ToString)
End While
ToolStripStatusLabel1.Text = "Periods Loaded!"
End Sub
答案 0 :(得分:1)
1)您在代码中添加了一个名为TeacherId的参数,但在您的proc上它并不存在。您必须将正确的参数从接口
传递给过程2)您的过程称为spGetPeriod,但在您的SQL上,您已选择*来自教师</ p>
答案 1 :(得分:0)
您已在sproc中将参数定义为“@Period”,但是当您将该参数添加到命令时,您将其称为“@TeacherId”。您的代码一目了然,尝试改变程序......
ALTER PROCEDURE spGetPeriod @TeacherId varchar(10)
AS
SELECT Period
FROM tmTeacher
WHERE TeacherId = @TeacherId
RETURN
对于没有任何数据的问题,可能尝试将组合框的数据源绑定到阅读器而不是循环读取器并添加每个数据,保留ExecuteReader语句并尝试用此结束替换你的while循环...
cboPeriod.DataSource = Reader
cboPeriod.DataTextField = "Period"
cboPeriod.DataValueField = "Period"
cboPeriod.DataBind()
答案 2 :(得分:0)
我发现您的架构存在一些问题:
花时间了解数据库规范化概念,如First,Second和Third Normal形式。如果你不遵守基本的标准化原则,你就会在未来遇到重大问题,以后修复这些问题将非常昂贵。