如何将参数传递给 SqlDataAdapter
Posted
技术标签:
【中文标题】如何将参数传递给 SqlDataAdapter【英文标题】:How to pass parameters to SqlDataAdapter 【发布时间】:2014-06-03 19:40:23 【问题描述】:我有一个 Vb.net 程序,它查询数据库以获取一堆记录。我不太清楚如何传递参数。以下是我的代码:
Dim connectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sql As String
Private Function GetCustomerData() As DataTable
locationdb = "10.0.1.1"
connectionString = ("Data Source=" & locationdb & ";Initial Catalog=TestDB;Persist Security Info=True;User ID=user;Password=password")
sql = ("SELECT lCustomerID,CustomerName,address FROM customers where @active = True...ETC")
sqlCnn = New SqlConnection(connectionString)
Dim CategoryAdapter As New SqlDataAdapter(sql, sqlCnn)
Dim CustomerInfo As New DataSet()
sqlCmd.Parameters.AddWithValue("@StartDate", frmMain.Startdate)
sqlCmd.Parameters.AddWithValue("@EndDate", frmMain.Enddate)
sqlCmd.Parameters.AddWithValue("@Department", "ALL")
sqlCmd.Parameters.AddWithValue("@Active", "1")
sqlCmd.Parameters.AddWithValue("@Visits", "ALL")
CategoryAdapter.Fill(CustomerInfo, "Customers")
Return CustomerInfo.Tables(0)
End Function
我需要通过:
@stardate
@enddate
@Deparment
@Active
@Visits
我收到错误:
NullReferenceException was unhandled. Object reference not set to an instance of an object.
在线:
sqlCmd.Parameters.AddWithValue("@StartDate", frmMain.Startdate)
frmMain.Startdate
和 frmMain.Enddate
由 frmMain
上的日期时间选择器 datetimepicker1
and datetimepicker2
定义
【问题讨论】:
您是否完成或执行了 GOOGLE 对 SqlDataAdapter 和 Parameters.AddWithValue() 方法的搜索 这其实很简单,我会将我的 C# 方法转换为 VB,您可以将其用作模板/指南,下次尝试显示更多的努力 msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx msdn.microsoft.com/en-us/library/… 太容易了 我将编辑我的帖子。我整个下午都在尝试,但我继续遇到错误。也许我应该张贴我得到的地方,而不是张贴我的“重新开始”点 【参考方案1】:这里是一个例子,你可以使用什么以及如何传递参数 您必须在必要时进行更改
Public Shared Function GetCustomerInfo(stardate As DateTime, enddate As DateTime, Department As String, Active as String, Visits as Int33) As List(Of String)
Dim cszList = New List(Of String)()
Dim DSCityStateZipLookup As New DataSet()
'load the List one time to be used thru out the intire application
Dim ConnString = System.Configuration.ConfigurationManager.ConnectionStrings("CMSConnectionString").ConnectionString
Using connStr As New SqlConnection(ConnString)
Using cmd As New SqlCommand("your Stored Proc name goes here", connStr)
cmd.Parameters.AddWithValue("@stardate", stardate)//make sure you assign a value to startdate
cmd.Parameters.AddWithValue("@enddate", enddate)//make sure you assign a value to enddate
cmd.Parameters.AddWithValue("@Deparment", Deparment)//make sure you assign a value to //Department
cmd.Parameters.AddWithValue("@Active", Active)//make sure you assign a value to Active
cmd.Parameters.AddWithValue("@Visits", Visits)//make sure you assign a value to Visits
cmd.Connection.Open()
New SqlDataAdapter(cmd).Fill(DSCityStateZipLookup)
'If we get a record back from the above stored procedure call, that in itself means the information the user provided from
'the UI is in the database. On the other hand, if we do not get a record back from the stored procedure call, we should
'simply advise the user that the information they provided does not exist in the database, and to double check their spelling.
If DSCityStateZipLookup.Tables.Count = 0 OrElse (DSCityStateZipLookup.Tables.Count > 0 AndAlso DSCityStateZipLookup.Tables(0).Rows.Count = 0) Then
cszList.Add("Your Error Message goes here if any.")
End If
End Using
End Using
Return cszList
End Function
【讨论】:
我没有传入 SQL 注入代码 我创建了一个存储过程 研究代码示例并从 ConnString 向下执行代码并替换以及添加缺少的参数 @LarsTech 感谢您的编辑 是的,对于存储过程,将存储过程作为第一个参数传递给sqlCommand
这里:msdn.microsoft.com/en-us/library/… 或设置CommandText
属性。根据 CTOR msdn.microsoft.com/en-us/library/877h0y3a%28v=vs.110%29.aspx
prokchop 你在做什么你不能看发布的代码
'New SqlDataAdapter(cmd).Fill(DSCityStateZipLookup)' 是 vb.net 中的语法错误
应该是Dim DBA = New SqlDataAdapter(cmd)...
或Using
。或者好吧......另外,sqlCommand
上的 Using
块是不必要的。打开连接也是多余的。数据适配器会这样做。以上是关于如何将参数传递给 SqlDataAdapter的主要内容,如果未能解决你的问题,请参考以下文章