iT邦幫忙

0

datatable運算控制的問題

ektrontek 2 周前2000 瀏覽

請教大家
要如何控制兩個datatable來做相差值的運算呢?
Dim ad As New SqlDataAdapter("select * from tmp where id='" & id & &''", con)
ad.Fill(dt)
Dim ad As New SqlDataAdapter("select * from tmp2 where id='" & id & &''", con)
ad.Fill(dt2)
會有兩個datatable,但我最後會透過副程式將傳回的sum的值
dt2.rows(i)("data")-dt.rows(i)("data")=sum

凡請大家指點迷津
感謝

小魚 iT邦新手 2 級 ‧ 2 周前 檢舉
不是應該是類似 sum = dt2.rows(i)("data")-dt.rows(i)("data") 嗎?
sam0407 iT邦高手 3 級 ‧ 1 周前 檢舉
因為看到您要用rows(i)去取資料,所以應該是多筆資料吧~
dt1與dt2傳回的筆數確定都相等嗎?
我的話SQL會這樣下:
select sum(data) from tmp where id='id'
select sum(data) from tmp2 where id='id'
然後作兩個指令傳回值的相減就好
ektrontek iT邦研究生 5 級 ‧ 1 周前 檢舉
我使用下列的語法,它傳回值是0,是否有其他原因呢?謝謝
Public Shared Function getproductcount(ByVal no As String) As Integer

Dim conString As String = "Server=192.168.8.202;Port=5432; Userid=user;Password=tmp; Protocol=3;SSL=false;Pooling=true;Mi" & _
"nPoolSize=1; MaxPoolSize=20;Encoding=UNICODE; Timeout=15;SslMode=Disable;Database=confluence"
Dim ourDataTable As DataTable = Nothing
Dim ourDataTable2 As DataTable = Nothing
Dim connection As Npgsql.NpgsqlConnection = New Npgsql.NpgsqlConnection(conString)
Try
ourDataTable = New DataTable
Dim ourAdapter As Npgsql.NpgsqlDataAdapter = New NpgsqlDataAdapter("select b.spacekey,count(1) from content a,spaces b where a.spaceid = b.spaceid and a.contenttype = 'PAGE' and a.content_status = 'current' and a.prevver is null and spacekey='" & no & "' group by b.spacekey ", connection)
'ourAdapter.SelectCommand.Parameters.AddWithValue("@no", no)
Dim ourAdapter2 As Npgsql.NpgsqlDataAdapter = New NpgsqlDataAdapter("select id4 from log2 where id3='" & no & "' and id2=7", connection)
'ourAdapter2.SelectCommand.Parameters.AddWithValue("@no", no)
' ourAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
ourAdapter.Fill(ourDataTable)
ourAdapter2.Fill(ourDataTable2)
Udf.AlertMessage(ourDataTable2.Rows(0)("id4") & "<br>")
If IsDBNull(no) Then
Return ""
Else
Return ourDataTable2.Rows(0)("id4") - ourDataTable.Rows(0)("count")
End If
Catch ex As Exception
End Try
End Function

尚未有邦友回答

立即登入回答