急求解决方法!!!请高手指教!

天蝎阿辉 2006-09-27 09:53:02
Dim con As New OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb"
con.Open()
Dim com As OleDbCommand = New OleDbCommand
com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between DateAdd("m",-10,getdate()) and getdate()"
TextBox1.Text = com.ExecuteScalar().ToString()
com.Dispose()
con.Close()
一开始会出现提示错误com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between DateAdd(""m"",-10,getdate()) and getdate()" 没有结束语句
我把这句改成com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between DateAdd(""m"",-10,getdate()) and getdate()"
可以运行但是点击计算按钮就会出现异常:ExecuteScalar: Connection 属性尚未初始化。

请高手指教~~
...全文
139 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
天蝎阿辉 2006-09-29
  • 打赏
  • 举报
回复
还是会出现同样的提示未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中
TextBox1.Text = com.ExecuteScalar().ToString()
天蝎阿辉 2006-09-27
  • 打赏
  • 举报
回复
以上方法都试过了~~还是不行~TextBox1.Text = com.ExecuteScalar().ToString()这个有异常
~未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
ggygd 2006-09-27
  • 打赏
  • 举报
回复
檢查下這句是不是有問題:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb"
Samen168 2006-09-27
  • 打赏
  • 举报
回复
com.Connection= con
kandyasp 2006-09-27
  • 打赏
  • 举报
回复
友情帮顶
ralpha08 2006-09-27
  • 打赏
  • 举报
回复
com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between DateAdd("m",-10,getdate()) and getdate()"
com.Connection= con
TextBox1.Text = com.ExecuteScalar().ToString()
Knight94 2006-09-27
  • 打赏
  • 举报
回复
Sample code as follows:
'Open connection

Dim com As OleDbCommand = New OleDbCommand
com.Connection = con
com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between @StartDate and @EndDate"

com.Parameters.Add( "@StartDate", DateTime.Now.AddMonths( -10 ) )
com.Parameters.Add( "@StartDate", DateTime.Now )

TextBox1.Text = com.ExecuteScalar().ToString()
com.Dispose()
con.Close()
天蝎阿辉 2006-09-27
  • 打赏
  • 举报
回复
还是有同样的错误!谢谢!
Knight94 2006-09-27
  • 打赏
  • 举报
回复
to com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between DateAdd("m",-10,getdate()) and getdate()"

Access数据库不支持“GetDate”和“DateAdd”这两个函数,用参数来实现

例如:
com.CommandText = "SELECT Sum(金额) FROM a WHERE 日期 Between @StartDate and @EndDate"

com.Parameters.Add( "@StartDate", DateTime.Now.AddMonths( -10 ) )
com.Parameters.Add( "@StartDate", DateTime.Now )

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧