【问题标题】:oracle vb.net dataset filloracle vb.net 数据集填充
【发布时间】:2023-03-20 03:04:01
【问题描述】:

这段代码有什么问题? 没有错误,但没有结果。我的 DataGridView_sent 没有被填满。

Dim objConn As New System.Data.OracleClient.OracleConnection
Dim objCmd As New System.Data.OracleClient.OracleCommand
Dim dtAdapter As New System.Data.OracleClient.OracleDataAdapter

Dim ds As New DataSet
Dim strConnString, strSQL As String

strConnString = "Data Source=db;User Id=user;Password=pass;"
strSQL = "select * from table where sentdate between '" & date1 & "' and '" & date2 & "'"

 objConn.ConnectionString = strConnString
 With objCmd
     .Connection = objConn
     .CommandText = strSQL
     .CommandType = CommandType.Text
  End With
  dtAdapter.SelectCommand = objCmd

  dtAdapter.Fill(ds)
  DataGridView_sent.DataSource = ds

  dtAdapter = Nothing
  objConn.Close()
  objConn = Nothing

【问题讨论】:

  • 检查“ds”中的值(行),数据库中是否有数据?并且还需要检查该过滤器的查询,即“发送日期在'”& date1&“'和'”& date2&“'””

标签: .net oracle datagridview dataset


【解决方案1】:

这可能是由于日期表示不正确造成的。
您应该使用参数化查询并让 Oracle Provider 以正确的模式呈现您的日期字段值

....
strSQL = "select * from table where sentdate between :date1 and :date2" 
objConn.ConnectionString = strConnString 
With objCmd 
   .Connection = objConn 
   .CommandText = strSQL 
   .CommandType = CommandType.Text 
   .Parameters.AddWithValue(":date1", Convert.ToDateTime(date1));
   .Parameters.AddWithValue(":date2", Convert.ToDateTime(date2));
End With 
dtAdapter.SelectCommand = objCmd 
.....

【讨论】:

  • 问题不在这里。还是一样的结果。有了这个 MsgBox(ds.Tables.Count) 我得到 '1' 即使有更多的行。
  • 哦,这给了我写的数字,这意味着数据集已填充。所以这条线 DataGridView_sent.DataSource = ds 不工作。我如何将 datagridview 与数据集?谢谢
  • dtAdapter.Fill(ds, "Sent") DataGridView_sent.DataSource = ds.Tables("Sent") 所以它起作用了。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2011-09-26
  • 2018-02-13
  • 1970-01-01
  • 2011-06-07
  • 2020-06-23
  • 1970-01-01
相关资源
最近更新 更多