大家好我的当前系统有问题。
在我的表单中使用此查询可以正常工作。
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
'Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"
Dim cmdconn2 = New SqlConnection
cmdconn2 = New SqlConnection
cmd = New SqlCommand
cmdconn2.ConnectionString = sqlstr
cmd.Connection = cmdconn2
cmdconn2.Open()
If CheckForText1() = False Then
MessageBox.Show("Cannot search with an empty value!")
frmVetsearch.Dispose()
Exit Sub
Else
cmd.CommandText = "select a.lname, a.fname, a.mi, a.vdms_no, b.lname, b.fname, b.mi, b.CLAIM_NO, c.ADDRESS1, c.ADDRESS2, c.ADDRESS3, c.sex, c.SCHOOL, c.C_YEAR, c.COURSE," & _
"c.telephone_no, c.mobile_no, c.email_add, c.EFFECTIVITY_DATE, c.SCC_NO, c.MLIST_NO, c.CATEGORY, c.DATE_FILED, c.relation_to_veteran, c.ENTITLEMENT_AWARD," & _
"c.REMARKS, c.UPDATED_BY, c.D_UPDATED from carlos.claims_veteran a left join carlos.claims_waivee b on a.vdms_no = b.vdms_no left join tobee.EBD_SCHBILLS c " & _
"on b.claim_no = c.CLAIM_NO where a.lname like '" + txtLname.Text + "%' and a.fname like '%" + txtFname.Text + "%' and a.mi like '%" + txtMi.Text + "%' " & _
"and a.VDMS_no like '" + txtVDMSNo.Text + "%'"
btnsearch.Enabled = False
btnSearchStud.Enabled = False
frmVetsearch.ShowDialog()
cmdconn.Close()
End If
End Sub
现在,我正在尝试通过将此查询转移到存储过程来改进我的系统。 我有可能做那件事。如果有可能请给我一个指南或解决方案?在此先感谢:)
答案 0 :(得分:2)
首先,您需要在Visual Studio中使用Sql Server Management Studio或Server Explorer窗口create the stored procedure
CREATE PROCEDURE SelectClaims
(
@fname nvarchar(max),
@lname nvarchar(max),
@mi nvarchar(max),
@vdms nvarchar(max)
)
as
select
a.lname, a.fname, a.mi, a.vdms_no, b.lname, b.fname, b.mi, b.CLAIM_NO,
c.ADDRESS1, c.ADDRESS2, c.ADDRESS3, c.sex, c.SCHOOL, c.C_YEAR, c.COURSE,
c.telephone_no, c.mobile_no, c.email_add, c.EFFECTIVITY_DATE, c.SCC_NO, c.MLIST_NO,
c.CATEGORY, c.DATE_FILED, c.relation_to_veteran, c.ENTITLEMENT_AWARD,
c.REMARKS, c.UPDATED_BY, c.D_UPDATED
from carlos.claims_veteran a left join carlos.claims_waivee b on a.vdms_no = b.vdms_no
left join tobee.EBD_SCHBILLS c On b.claim_no = c.CLAIM_NO
where a.lname like @lname and a.fname like @fname and a.mi like @mi and a.VDMS_no like @vdms
正如您所看到的,此sp接收4个参数,所有类型都为nvarchar
,并且我已将其大小设置为最大可能值(您应该将此值微调为相关字段的有效大小)< / p>
然后在VB.NET中使用此代码调用此SP
If CheckForText1() = False Then
MessageBox.Show("Cannot search with an empty value!")
frmVetsearch.Dispose()
Exit Sub
Else
Using cmdconn2 = New SqlConnection(sqlstr)
Using cmd = New SqlCommand("SelectClaims", cmdconn2)
cmdconn2.Open()
cmd.Parameters.AddWithValue("@lname", txtLname.Text & "%")
cmd.Parameters.AddWithValue("@fname", "%" & txtFname.Text & "%"
cmd.Parameters.AddWithValue("@mi", "%" & txtMi.Text & "%"
cmd.Parameters.AddWithValue("@vdms", txtVDMSNo.Text & "%"
cmd.CommandType = CommandType.StoredProcedure
.... you are missing the code that read back your values
Using reader = cmd.ExecuteReader()
.......
End Using
End Using
End Using
End If
这里重要的事情是
CommandType.StoredProcedure
以允许
正确处理CommandText设置为存储的名称
程序而不是sql语句。但是我希望用这种方法警告你。在这样的选择中,使用存储过程,不能明确地利用正确的参数化查询。从我的角度来看,我发现使用和维护大量存储过程很容易成为真正的维护噩梦。
例如,假设您的程序的新版本需要检索新字段 现在,您需要更新代码和存储过程。并且不要忘记,在某些环境中,您需要请求专职人员(A DBA)的帮助才能对生产中的数据库进行任何更改。