【发布时间】: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,您可以将其用作模板/指南,下次尝试显示更多的努力
-
我将编辑我的帖子。我整个下午都在尝试,但我继续遇到错误。也许我应该张贴我得到的地方,而不是张贴我的“重新开始”点
标签: sql vb.net sqlconnection sqldataadapter