77 lines
3.6 KiB
VB.net
77 lines
3.6 KiB
VB.net
Imports System.IO
|
|
Imports Microsoft.Data.SqlClient
|
|
Imports System.Data.DataTable
|
|
|
|
Public Class Form10
|
|
|
|
Dim DataString As String = My.Settings.subnet
|
|
Dim surce As String = My.Settings.serverName
|
|
Dim catalog As String = My.Settings.serverdatabase
|
|
Dim user As String = My.Settings.serverUser
|
|
Dim pass As String = My.Settings.serverUserpass
|
|
Dim timeout As String = My.Settings.timeout
|
|
Dim encrypt As String = My.Settings.encrypt
|
|
Dim trust As String = My.Settings.cert
|
|
Dim reason As String = My.Settings.reason
|
|
Dim subnet As String = My.Settings.subnet
|
|
|
|
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
|
' this button handle the SQL consction for filling the data grid view with information from SQL database.
|
|
Dim con As New SqlConnection("Data Source=" & surce & "; Initial Catalog=" & catalog & "; User ID=" & user &
|
|
"; Password=" & pass & "; Connect Timeout=" & timeout & "; Encrypt=" & encrypt &
|
|
"; TrustServerCertificate=" & trust & "; ApplicationIntent=" & reason & "; MultiSubnetFailover=" & subnet & "")
|
|
|
|
Dim command As New SqlCommand("select * from calitate.dbo.mentenanta where [Data] between @date1 and @date2", con)
|
|
command.Parameters.Add("date1", SqlDbType.Date).Value = DateTimePicker1.Value ' date from to be selected
|
|
command.Parameters.Add("date2", SqlDbType.Date).Value = DateTimePicker3.Value ' date to to be selected
|
|
Dim da As New SqlDataAdapter ' Sql adaptor for table
|
|
con.Open()
|
|
da.SelectCommand = command ' sql command for downnload
|
|
Dim dt As New DataTable ' sql table preparing for Datagrig
|
|
dt.Clear() ' clear table adaptor
|
|
da.Fill(dt) ' fill table adaptor
|
|
DataGridView1.DataSource = dt ' filling the data grid view
|
|
con.Close()
|
|
End Sub
|
|
|
|
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
|
|
'Button for saving the information from data grid view to CSV
|
|
Dim saveFileDialog As New SaveFileDialog()
|
|
saveFileDialog.Filter = "CSV (.csv)|.csv" 'save fiel dialog
|
|
saveFileDialog.FileName = "export.csv" ' save file type
|
|
If (saveFileDialog.ShowDialog() = DialogResult.OK) Then ' check if name for file exist
|
|
Using sw As New StreamWriter(saveFileDialog.FileName) ' comand for rithing CSV file
|
|
Dim columnCount As Integer = DataGridView1.ColumnCount
|
|
For i As Integer = 0 To columnCount - 1
|
|
sw.Write(DataGridView1.Columns(i).HeaderText)
|
|
If (i < columnCount - 1) Then
|
|
sw.Write(","c)
|
|
End If
|
|
Next
|
|
sw.Write(sw.NewLine)
|
|
For Each row As DataGridViewRow In DataGridView1.Rows
|
|
For i As Integer = 0 To row.Cells.Count - 1
|
|
If Not row.IsNewRow Then
|
|
sw.Write(row.Cells(i).Value.ToString())
|
|
End If
|
|
If (i < row.Cells.Count - 1) Then
|
|
sw.Write(","c)
|
|
End If
|
|
Next
|
|
sw.Write(sw.NewLine)
|
|
Next
|
|
sw.Close()
|
|
End Using
|
|
MessageBox.Show("CSV file saved.") ' message that file was succesfuly created
|
|
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
|
|
Form1.Show()
|
|
Me.Close()
|
|
End Sub
|
|
End Class
|
|
|
|
|