我的一个查询语句,再没有使用存储过程时,用的好好的,使用存储过程后出现:未将对象引用设置到对象的实例
我的一个查询语句,再没有使用存储过程时,用的好好的,使用存储过程后出现:未将对象引用设置到对象的实例
Dim strsql As String = "select tbsptm.f_sptm sptm,tbspda.f_spmc spmc,c.qmsl, " & _
"(c.qmsl*tbspda.f_xsdj) qmje,(tbspda.f_zhjj*(1+tbspda.f_sl/100)) zhjj,tbspda.f_xsdj xsdj,c.qmsl*(tbspda.f_zhjj*(1+tbspda.f_sl/100)) qmjjje from " & _
"(select a.f_spbm spbm,(a.qcsl+b.fssl) qmsl from (select f_spbm,sum(f_qcsl) qcsl,sum(f_qcje) qcje from " & bbb & "_yhj where f_bmbm like ' '% ' ' and f_spbm in " & _
"(select f_spbm from tbsptm where f_xh= ' '1 ' ' and f_sptm like ' '% ' ' and f_spbm in (select f_spbm from tbgysdz where f_gysbm= ' ' " & userid & " ' ')) group by f_spbm) a, " & _
"(select f_spbm,(sum(f_jhsl)+sum(f_Brsl)+sum(f_Sysl)+sum(f_Pssl)-sum(f_Xssl)-sum(f_Bcsl)-sum(f_Shsl)-sum(f_Phsl)) fssl from " & bbb & "_rhj where f_rq <= ' ' " & rq2 & " ' ' " & _
"and f_bmbm like ' '% ' ' and f_spbm in (select f_spbm from tbsptm where f_xh= ' '1 ' ' and f_sptm like ' '% ' ' and f_spbm in (select f_spbm from tbgysdz where f_gysbm= ' ' " & userid & " ' ')) " & _
"group by f_spbm) b where a.f_spbm=b.f_spbm) c,tbspda,tbsptm where c.spbm=tbspda.f_spbm and c.spbm=tbsptm.f_spbm and tbsptm.f_xh= ' '1 ' ' and f_sptm like ' '% ' ' order by tbsptm.f_sptm "
strsql = "exec ProcGetPage ' " & strsql & " ', '1 ', '10 ' "
dr = hsjdata.readrow(strsql)
Dim dt As DataTable = New DataTable
dt = hsjdata.ConvertDataReaderToDataTable(dr)
dt.DefaultView.Sort = ViewState( "SortField ") + " " + ViewState( "SortDirect ")
dgDocList.DataSource = dt.DefaultView
dgDocList.DataBind()
_______________________________________________________
Public Shared Function ConvertDataReaderToDataTable(ByVal dataReader As SqlDataReader) As DataTable
Dim datatable As DataTable = New DataTable
***************************************
未将对象引用设置到对象的实例 Dim schemaTable As DataTable = dataReader.GetSchemaTable()*************
Try
For Each myRow As DataRow In schemaTable.Rows
Dim myDataColumn As DataColumn = New DataColumn
myDataColumn.DataType = myRow.GetType
myDataColumn.ColumnName = myRow(0).ToString
datatable.Columns.Add(myDataColumn)
Next
'While dataReader.Read
Do
Dim myDataRow As DataRow = datatable.NewRow
Dim i As Integer = 0
While i < schemaTable.Rows.Count
myDataRow(i) = dataReader(i).ToString
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
datatable.Rows.Add(myDataRow)
myDataRow = Nothing
Loop While dataReader.Read
'End While
schemaTable = Nothing
dataReader.Close()
Return datatable
Catch ex As Exception
'Error .Log(ex.ToString)
Throw New Exception( "转换出错出错! ", ex)
End Try
End Function
[解决办法]
在查询分析器里运行存储过程看看有不有数据返回
[解决办法]
dr = hsjdata.readrow(strsql)==> 看看dr是不是null?
[解决办法]
Dim schemaTable As new DataTable = dataReader.GetSchemaTable()
[解决办法]
LZ:
是加了
strsql = "exec ProcGetPage ' " & strsql & " ', '1 ', '10 ' "
以后出问题了么?
------------
还是其它?
[解决办法]
dr = hsjdata.readrow(strsql)
打印出这句
strsql = "exec ProcGetPage ' " & strsql & " ', '1 ', '10 ' "
在查询分析器里执行
应该没有返回结果集
[解决办法]
你是把sql传到一个存储过程里面动态执行?是不是某个地方 '号忘记换了?运行sql跟踪器跟踪一下发到数据库的 sql看看/
[解决办法]
---------
你把
strsql = "exec ProcGetPage ' " & strsql & " ', '1 ', '10 ' "
改
strsql = "exec dbo.ProcGetPage ' " & strsql & " ', '1 ', '10 ' "
[解决办法]
Dim schemaTable As DataTable = New DataTable
schemaTable = dataReader.GetSchemaTable()
------解决方案--------------------
未将对象引用设置到对象的实例
意思是你某个对象没有正确实例化
不是你sql语句的问题
是你定义的对象的问题
仔细检查下代码吧
Dim schemaTable As DataTable 没有 new 直接使用的话会导致实例化错误
你看看修改后是不是其他地方报错
[解决办法]
应该是这里dataReader.GetSchemaTable出错
[解决办法]
dataReader是否实例化?
[解决办法]
dim dataReader as sqldatareader = null
datareader应该这样实例化
[解决办法]
up
[解决办法]
dr = hsjdata.readrow(strsql)dr = dr.NextResult(); //加一句
[解决办法]
学习下。
[解决办法]
我写错了dr.NextResult(); 就可,不要返回值
[解决办法]
存储过程 能这样掉用 ?