2015-04-19 61 views
1

我已经插入了两行,表中的内容相同,但仍然存在用于搜索现有数据的代码未返回所需结果。检查SQL Server数据库中是否存在使用存储过程的行

这是我的VB.Net代码:

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click 

    Try 

If DateTimePicker1.Value.Date >= DateTime.Parse(TxtDateFrom.Text) And DateTimePicker1.Value.Date <= DateTime.Parse(TxtDateTo.Text) Then 


    Dim cmd1 As New SqlCommand 
      cmd1 = New SqlCommand("check_sched", connection) 
      cmd1.CommandType = CommandType.StoredProcedure 
      Dim rd As SqlDataReader 
      cmd1.Parameters.AddWithValue("@empid", Label17.Text) 
      cmd1.Parameters.AddWithValue("@datesched", DateTimePicker1.Value.Date) 
      cmd1.Parameters.AddWithValue("@timefrom", TimePicker1.Text) 
      cmd1.Parameters.AddWithValue("@timeto", TimePicker1.Text) 
      rd = cmd1.ExecuteReader() 


      If rd.HasRows Then 

       MsgBox("Employee already assigned a job for the particular time period", MsgBoxStyle.Information, "Change Time Period") 

      Else 

       Dim cmd As New SqlCommand 
       cmd = New SqlCommand("insert_schedule1", connection) 
       cmd.CommandType = CommandType.StoredProcedure 
       cmd.Parameters.Add("@actid", SqlDbType.Int).Value = Label2.Text 
       cmd.Parameters.Add("@activity", SqlDbType.NVarChar).Value = CmbActivity.Text 
       cmd.Parameters.Add("@serviceid", SqlDbType.Int).Value = Label14.Text 
       cmd.Parameters.Add("@servicename", SqlDbType.NVarChar).Value = TxtServiceType.Text 
       cmd.Parameters.Add("@pdatefrom", SqlDbType.NVarChar).Value = TxtDateFrom.Text 
       cmd.Parameters.Add("@pdateto", SqlDbType.NVarChar).Value = TxtDateTo.Text 
       cmd.Parameters.Add("@deptname", SqlDbType.NVarChar).Value = Label16.Text 
       cmd.Parameters.Add("@emp", SqlDbType.NVarChar).Value = CmbEmp.Text 
       cmd.Parameters.Add("@empid", SqlDbType.Int).Value = Label17.Text 
       cmd.Parameters.Add("@datesched", SqlDbType.DateTime).Value = myDate1 
       cmd.Parameters.Add("@timefrom", SqlDbType.Time).Value = TimePicker1.Value.TimeOfDay 
       cmd.Parameters.Add("@timeto", SqlDbType.Time).Value = TimePicker2.Value.TimeOfDay 
       cmd.Parameters.Add("@status", SqlDbType.Int).Value = "1" 
       cmd.ExecuteNonQuery() 
       MsgBox("Scheduled Successfully", MsgBoxStyle.Information, "Task Schedule") 
       CmbActivity.Text = "" 
       TxtServiceType.Text = "" 
       TxtDateFrom.Text = "" 
       TxtDateTo.Text = "" 
       'CmbSactivity.Text = "" 
       CmbEmp.Text = "" 
       DateTimePicker1.Text = Now 
       TimePicker1.Text = TimeOfDay 
       TimePicker2.Text = TimeOfDay 
      End If 
     Else 
      MsgBox("Please select a date within the duration", MsgBoxStyle.Information, "Invalid Date") 
     End If 

    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 

谁能帮我找到这个错误吗?

这是我的存储过程

create procedure check_sched 
    @empid int, 
    @datesched datetime, 
    @timefrom datetime, 
    @timeto datetime 
as 
begin 
    SELECT * 
    FROM tbl_schedule 
    WHERE empid = @empid 
     AND datesched = @datesched 
     AND timefrom >= @timefrom 
     AND timeto <= @timeto 
end 

表模式:

CREATE TABLE [dbo].[tbl_schedule] 
( 
    [actid] [int] NULL,  
    [activity] [nvarchar](350) NULL, 
    [serviceid] [int] NULL,  
    [servicename] [nvarchar](50) NULL,  
    [pdatefrom] [nvarchar](50) NULL, 
    [pdateto] [nvarchar](50) NULL,  
    [sactid] [int] NULL, 
    [sactivity] [nvarchar](500) NULL, 
    [deptname] [nvarchar](150) NULL, 
    [emp] [nvarchar](150) NULL,  
    [empid] [int] NULL,  
    [datesched] [datetime] NULL, 
    [timefrom] [datetime] NULL,  
    [timeto] [datetime] NULL, 
    [actimefrom] [datetime] NULL, 
    [actimeto] [datetime] NULL, 
    [completed] [int] NULL,  
    [status] [int] NULL 
) ON [PRIMARY] 
+0

请提供表格结构和预期结果。 –

+0

用表定义问题现在面临的问题是这段代码没有显示任何错误,但是如果我输入两个字段具有相同的empid,日期时间,timefrom和timeto,它应该显示员工已经分配给另一个工作的消息框在那段时间。 – Hrishi

+0

而是将数据再次添加到显示消息框“预定成功”的数据库中 – Hrishi

回答

4

你的检查时间表同时用于@timeFrom@timeTo领域TimePicker1价值,同时您插入分别使用TimePicker1TimePicker2。这会导致您的支票查看错误的日期。

为了避免将来出现这个问题,并为了更容易找到这些错误,请尝试重构名称以匹配它们的用途(例如fromTimePickertoTimePicker)。

+0

此外,我建议检查员工是否已在INSERT存储过程中的该时间段内分配给另一个工作,并让它返回一个位输出参数指示成功或失败。 –

相关问题