我有一个搜索数据的存储过程,它在SQL Server 2008中完美运行。
我的存储过程代码:
ALTER PROCEDURE [dbo].[uspSearch]
@SurName NVarchar(20)
AS
BEGIN
SET NOCOUNT ON;
SELECT
SI.Surname,SI.FirstName,SI.MiddleName, SI.StudAddress ,
SI.BirthDay, SI.Gender, SI.Nationality, SI.BirthPlace,
SI.TelNum, SI.SchoolWhereGraduated ,
SI.DatesWhenGraduated, SI.SchoolLastAttended,
SI.Note, SI.StudImage,
PI.Father_FirstName, PI.Father_LastName,
PI.Father_MI, PI.Father_Occupation,
PI.Father_TelNUm, PI.Mother_FirstName, PI.Mother_LastName,
PI.Mother_MI, PI.Mother_Occupation, PI.Mother_TelNum,
PI.Contact_FirstName, PI.Contact_LastName, PI.Contact_MI,
PI.Contact_Mobile, PI.Contact_TelNum,
SH.SchoolYear, SH.Levels, SH.Section, SH.DateEnrolled
FROM
StudentInformation SI
JOIN
StudentHistory SH ON SI.StudentID = SH.StudentID
JOIN
ParentInformation PI ON PI.ParentID = SI.ParentID
WHERE
SI.Surname Like '%'+ @Surname+'%'
END
我的问题是当我从vb.net执行它时,它不返回任何数据,并且每当我点击搜索按钮时都没有错误消息。
有人可以帮我解决这个问题吗?提前谢谢。
我的vb.net代码
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Dim i As Integer
Dim a As New OpenFileDialog
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
cn.Open()
Using cmd As New SqlClient.SqlCommand("dbo.uspSearch", cn)
cmd.Parameters.AddWithValue("@SurName", txtSearch.Text)
dt.Clear()
da.Fill(dt)
dgv1.RowTemplate.Height = 50
dgv1.DataSource = dt
For i As Integer = 0 To dgv1.Columns.Count - 1
If TypeOf dgv1.Columns(i) Is DataGridViewImageColumn Then
DirectCast(dgv1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
End If
Next
End Using
cn.Close()
End Sub
答案 0 :(得分:2)
您应该通知SqlCommand您正在其CommandText属性中设置存储过程的名称
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
cn.Open()
Using cmd As New SqlClient.SqlCommand("dbo.uspSearch", cn)
' add this line'
cmd.CommandType = CommandType.StoredProcedure
Dim da = new SqlDataAdapter(cmd)
Dim dt = new DataTable()
da.Fill(dt)
....
End Using
cn.Close()
End Sub
CommandType的默认值是Text,因此预期用于命令文本的字符串是SELECT / INSERT / UPDATE等...而不是StoredProcedure的名称
答案 1 :(得分:1)
你应该有这样的东西
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
cn.Open()
Using cmd As New SqlClient.SqlCommand("dbo.uspSearch", cn)
cmd.Parameters.AddWithValue("@SurName", txtSearch.Text)
cmd.CommandType=CommandType.StoredProcedure
dt.Clear()
da.Fill(dt)
dgv1.RowTemplate.Height = 50
dgv1.DataSource = dt
For i As Integer = 0 To dgv1.Columns.Count - 1
If TypeOf dgv1.Columns(i) Is DataGridViewImageColumn Then
DirectCast(dgv1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
End If
Next
End Using
cn.Close()
End Sub
<强>更新强>
你可以改写
Dim cmd As New SqlCommand()
Dim da As New SqlDataAdapter()
Dim dt As New DataTable()
Try
cn.Open()
cmd = New SqlCommand("uspSearch", cn)
cmd.Parameters.AddWithValue("@SurName", txtSearch.Text)
cmd.CommandType = CommandType.StoredProcedure
da.SelectCommand = cmd
da.Fill(dt)
dgv1.DataSource = dt
Catch x As Exception
MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Finally
cmd.Dispose()
cn.Close()
End Try