一个有关的数据库读取与转换的错误!

lin9703 2002-10-09 02:48:23
我在VB。NET模块里自定义了一个函数如下:
Public Function ReadOneValue(ByVal mySql As String)
Dim myConnection As SqlConnection = New SqlConnection(strConnect)
Dim myCommand As New SqlCommand(mySql, myConnection)
Dim Ad As SqlDataAdapter = New SqlDataAdapter()
Try
Ad.SelectCommand = myCommand
Dim ds As DataSet = New DataSet()
Ad.Fill(ds)
ReadOneValue = ds.Tables(0).Rows(0).Item(0)
myConnection.Close()
Catch e As System.IndexOutOfRangeException
Console.Write(e.ToString)
ReadOneValue = ""
End Try
End Function
然后我在VB的WEB窗体下调用
PAGE_LOAD过程
pubsql="一个SQL语句"
if ReadOneValue(pubsql)<>"" then '这句话提示DBNULL 无法强制转换为String DBNULL<>""
textbox1.text=ReadOneValue("pubsql ")
end if
请各位高手指点错误!
...全文
48 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
GSDN 2002-10-09
  • 打赏
  • 举报
回复
ReadOneValue返回的是一个Object,而其实你需要的是一个字符串,所以需要定义函数返回类型为String,然后在赋值的时候做适当转换,如:ReadOneValue = CType(ds.Tables(0).Rows(0).Item(0), String)即可。当然也可用其他方法。

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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