I want to translate this vb.net code into sql query for optimizing the database connection (request time out)
Database
USE [dbBom]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JvHeader_ID] [int] NULL,
[Debit] [float] NULL,
[credit] [float] NULL,
[AC_Code_No] [int] NULL,
[cal] [float] NULL,
[PlantID] [int] NULL
) ON [PRIMARY]
GO
INSERT DATA
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2 ,76089,0,129,0.934579439252337,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 5326.23,0,17,0.0654205607476635,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 0,81415.23,48,1 ,1)
VB.NET CODE
'Please created 2 datagridviews which name are DataGridView1 And DataGridView2 at design form
Dim connetionString As String = "SomeDatabase"
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim sql As String
Dim dt3 As New DataTable
Dim dt1 As New DataTable
Dim SumDebit As Double = 0
Dim SumCredit As Double = 0
dt3.Columns.Add("AccodeNo", GetType(Integer))
'dt3.Columns.Add("Acname", GetType(String))
dt3.Columns.Add("Debit", GetType(String))
dt3.Columns.Add("Credit", GetType(String))
dt3.Columns.Add("Datasource", GetType(String))
dt3.Columns.Add("JVHeader_ID", GetType(Integer))
dt3.Columns.Add("PlantID", GetType(Integer))
sql = "SELECT JvHeader_ID, Debit, credit, Sum(Debit) over() As SumDebit , AC_Code_No, cal, PlantID FROM JV"
connection = New SqlConnection(connetionString)
connection.Open()
Command = New SqlCommand(Sql, connection)
adapter.Dispose()
adapter.SelectCommand = command
adapter.Fill(dt1)
Command.Dispose()
connection.Close()
DataGridView1.DataSource = dt1
For count = 0 To dt1.Rows.Count - 1
Dim array(10) As Integer
Dim n As Integer = 0
Dim add As Integer = 2
Dim cal(10) As Double
Dim Debit As Double = dt1.Rows(count).Item("Debit")
Dim Credit As Double = dt1.Rows(count).Item("Credit")
If Debit <> 0 Then
For i = 0 To dt1.Rows.Count - 1
If CDbl(dt1.Rows(i).Item("Credit")) > 0 Then
array(n) = dt1.Rows(i).Item("AC_CODE_NO")
'acname(n) = dt1.Rows(i).Item("AC_Name")
cal(n) = CDbl(dt1.Rows(i).Item("Cal"))
n = n + 1
End If
Next
ElseIf Credit <> 0 Then
For i = 0 To dt1.Rows.Count - 1
If CDbl(dt1.Rows(i).Item("Debit")) > 0 Then
array(n) = dt1.Rows(i).Item("AC_CODE_NO")
'acname(n) = dt1.Rows(i).Item("AC_Name")
cal(n) = CDbl(dt1.Rows(i).Item("Cal"))
n = n + 1
End If
Next
End If
For p = 0 To n - 1
If Credit = 0 Then
dt3.Rows.Add(array(p), cal(p) * CDbl(dt1.Rows(count).Item("SumDebit")) * CDbl(dt1.Rows(count).Item("Cal")), "", dt1.Rows(count).Item("Ac_code_no"), dt1.Rows(count).Item("JVHeader_ID"), dt1.Rows(count).Item("PlantID"))
ElseIf Debit = 0 Then
dt3.Rows.Add(array(p), "", cal(p) * CDbl(dt1.Rows(count).Item("SumDebit")) * CDbl(dt1.Rows(count).Item("Cal")), dt1.Rows(count).Item("Ac_code_no"), dt1.Rows(count).Item("JVHeader_ID"), dt1.Rows(count).Item("PlantID"))
End If
Next
Next
DataGridView2.DataSource = dt3
'---------------------------
The resut i expect will be at DataGridView2 ,
after this i insert dt3 into some table which have the same column in database .
Is it possible to do this , If it is possible , Will this method improve my program performance?
Thanks
Database
USE [dbBom]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JvHeader_ID] [int] NULL,
[Debit] [float] NULL,
[credit] [float] NULL,
[AC_Code_No] [int] NULL,
[cal] [float] NULL,
[PlantID] [int] NULL
) ON [PRIMARY]
GO
INSERT DATA
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2 ,76089,0,129,0.934579439252337,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 5326.23,0,17,0.0654205607476635,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 0,81415.23,48,1 ,1)
VB.NET CODE
'Please created 2 datagridviews which name are DataGridView1 And DataGridView2 at design form
Dim connetionString As String = "SomeDatabase"
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim sql As String
Dim dt3 As New DataTable
Dim dt1 As New DataTable
Dim SumDebit As Double = 0
Dim SumCredit As Double = 0
dt3.Columns.Add("AccodeNo", GetType(Integer))
'dt3.Columns.Add("Acname", GetType(String))
dt3.Columns.Add("Debit", GetType(String))
dt3.Columns.Add("Credit", GetType(String))
dt3.Columns.Add("Datasource", GetType(String))
dt3.Columns.Add("JVHeader_ID", GetType(Integer))
dt3.Columns.Add("PlantID", GetType(Integer))
sql = "SELECT JvHeader_ID, Debit, credit, Sum(Debit) over() As SumDebit , AC_Code_No, cal, PlantID FROM JV"
connection = New SqlConnection(connetionString)
connection.Open()
Command = New SqlCommand(Sql, connection)
adapter.Dispose()
adapter.SelectCommand = command
adapter.Fill(dt1)
Command.Dispose()
connection.Close()
DataGridView1.DataSource = dt1
For count = 0 To dt1.Rows.Count - 1
Dim array(10) As Integer
Dim n As Integer = 0
Dim add As Integer = 2
Dim cal(10) As Double
Dim Debit As Double = dt1.Rows(count).Item("Debit")
Dim Credit As Double = dt1.Rows(count).Item("Credit")
If Debit <> 0 Then
For i = 0 To dt1.Rows.Count - 1
If CDbl(dt1.Rows(i).Item("Credit")) > 0 Then
array(n) = dt1.Rows(i).Item("AC_CODE_NO")
'acname(n) = dt1.Rows(i).Item("AC_Name")
cal(n) = CDbl(dt1.Rows(i).Item("Cal"))
n = n + 1
End If
Next
ElseIf Credit <> 0 Then
For i = 0 To dt1.Rows.Count - 1
If CDbl(dt1.Rows(i).Item("Debit")) > 0 Then
array(n) = dt1.Rows(i).Item("AC_CODE_NO")
'acname(n) = dt1.Rows(i).Item("AC_Name")
cal(n) = CDbl(dt1.Rows(i).Item("Cal"))
n = n + 1
End If
Next
End If
For p = 0 To n - 1
If Credit = 0 Then
dt3.Rows.Add(array(p), cal(p) * CDbl(dt1.Rows(count).Item("SumDebit")) * CDbl(dt1.Rows(count).Item("Cal")), "", dt1.Rows(count).Item("Ac_code_no"), dt1.Rows(count).Item("JVHeader_ID"), dt1.Rows(count).Item("PlantID"))
ElseIf Debit = 0 Then
dt3.Rows.Add(array(p), "", cal(p) * CDbl(dt1.Rows(count).Item("SumDebit")) * CDbl(dt1.Rows(count).Item("Cal")), dt1.Rows(count).Item("Ac_code_no"), dt1.Rows(count).Item("JVHeader_ID"), dt1.Rows(count).Item("PlantID"))
End If
Next
Next
DataGridView2.DataSource = dt3
'---------------------------
The resut i expect will be at DataGridView2 ,
after this i insert dt3 into some table which have the same column in database .
Is it possible to do this , If it is possible , Will this method improve my program performance?
Thanks