我是编程新手,我尝试使用参数化查询创建一个包含SQL语句的子。我的代码不起作用,可能缺少某些东西,或者我做错了。
我的连接字符串位于app.config
:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name = "DBCS"
connectionString ="Data Source=Mic-PC\Developer; Initial Catalog=Customer; User Id = sa; Password=1224334;"
providerName ="System.Data.SqlClient" />
</connectionStrings>
</configuration>
我创建了一个名为SELECTCLASS
的类,这里是内容
Imports System.Configuration
Imports System
Imports System.Threading.Tasks
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class SelectClass
Public Sub searchData(search As String, LastName As String, FirstName As String, MiddleInitial As String, Age As String, Address As String)
Dim CS As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Using con As New SqlConnection(CS)
Dim cmd As SqlCommand = New SqlCommand("spGetCustomerByName", con)
cmd.CommandType = CommandType.Storedprocedure
cmd.Parameters.AddWithValue("@FirstName", "%" + search + "%")
con.Open()
Using rdr = cmd.ExecuteReader()
If rdr.HasRows Then
While rdr.Read()
LastName = rdr.GetString(1).ToString
FirstName = rdr.GetString(2).ToString
MiddleInitial = rdr.GetString(3).ToString
Age = rdr.GetString(4).ToString
Address = rdr.GetString(5).ToString
End While
End If
End Using
End Using
End Sub
End Class
以下是我的Windows窗体的代码:
Imports System.Configuration
Imports System
Imports System.Threading.Tasks
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class SELECTVB
Dim SelectData As New SelectClass
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
SelectData.searchData(txtsearch.Text, TextBox1.Text.ToString, TextBox2.Text.ToString, TextBox3.Text.ToString, TextBox4.Text.ToString, TextBox5.Text.ToString)
End Sub
非常感谢任何帮助,提前谢谢!
答案 0 :(得分:0)
我认为我的代码存在两个问题,这两个问题都与ADO或SQL无关。这些问题与从数据库中读取的数据传递回调用方法的方式有关。我怀疑searchData
方法是否成功地从查询结果中读取数据(尽管您需要通过设置断点,逐步调试方法以及检查变量值来使用调试器来确认这一点。 )。
我看到的第一个问题是您传递的是TextBox.Text.ToString
方法而不是TextBox.Text
属性本身的结果。换句话说,您应该像这样调用searchData
方法:
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
SelectData.searchData(txtsearch.Text, TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
End Sub
其次,您试图从Text
方法内部重新分配这些searchData
参数的引用,但参数未声明为ByRef
。如果你想这样做,你需要将方法的参数更改为ByRef
,如下所示:
Public Sub searchData(search As String, ByRef LastName As String, ByRef FirstName As String, ByRef MiddleInitial As String, ByRef Age As String, ByRef Address As String)
但是,如果通过函数的返回值返回结果而不是更改参数的值,则这些都不会成为问题。包括我自己在内的大多数开发人员都不鼓励以这种方式使用ByRef
参数,因为它是出乎意料的行为。通常,参数应保留用于输入而不是输出。例如,如果您创建了这样的类:
Public Class Customer
Public Property LastName As String
Public Property FirstName As String
Public Property MiddleInitial As String
Public Property Age As String
Public Property Address As String
End Class
然后您可以更改搜索方法以返回Customer
对象,如下所示:
Public Function SearchData(search As String) As Customer
Dim result As Customer = Nothing
Dim CS As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Using con As New SqlConnection(CS)
Using cmd As SqlCommand = New SqlCommand("spGetCustomerByName", con)
cmd.CommandType = CommandType.Storedprocedure
cmd.Parameters.AddWithValue("@FirstName", "%" & search & "%")
con.Open()
Using rdr = cmd.ExecuteReader()
If rdr.HasRows Then
If rdr.Read() Then
result = New Customer()
result.LastName = rdr.GetString(1)
result.FirstName = rdr.GetString(2)
result.MiddleInitial = rdr.GetString(3)
result.Age = rdr.GetString(4)
result.Address = rdr.GetString(5)
End If
End If
End Using
End Using
End Using
Return result
End Sub
然后你可以这样称呼它:
Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
Dim c As Customer = SelectData.searchData(txtsearch.Text)
If c IsNot Nothing Then
TextBox1.Text = c.LastName
TextBox2.Text = c.FirstName
TextBox3.Text = c.MiddleInitial
TextBox4.Text = c.Age
TextBox5.Text = c.Address
End If
End Sub
但是,考虑到你正在做的事情的背景,你的方法似乎应该返回一个客户列表而不仅仅是一个客户列表。
如果进行这些更改仍无法解决问题,则使用调试器逐步执行搜索方法将是解决问题的最佳机会。可能是存储过程没有返回匹配项,或者它返回的列数与您期望的不同,或者它可能返回一个数据类型不是字符串的列(如年龄列)例如,可能是一个整数。仅仅根据您提供的信息,数据访问代码的确切问题是什么,这是不可能的。