-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLreader.vb
More file actions
105 lines (90 loc) · 4.43 KB
/
SQLreader.vb
File metadata and controls
105 lines (90 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Namespace sqlreading
Public Class sqltables
'creates a table and displays the data to html table
Public Function DisplayRow() As String
Dim data As String = "<table border='1' class='table' style='table-layout: fixed; width: 100%'> <tr><th>Questions</th> <th>Answers</th> <th>Keywords</th> <th>Options</th> </tr>"
Dim sql_command As String = "Select * FROM [Answers] "
Dim con As New SqlConnection("Server=DESKTOP-3J246S1\SQLEXPRESS01;Database=Questions;User Id=kostas;Password=12345rondo;")
Using cmd As New SqlCommand(sql_command, con)
con.Open()
Using r As SqlDataReader = cmd.ExecuteReader()
Dim id = 0
While r.Read
data += "<tr class='editable'>"
data += addColumn(r("Questions"))
data += addColumn(r("Answers"))
data += addColumn(r("Keywords"))
data += addColumn("<button id=" & id & " type='button' class='editbtn' class='btn btn-outline-primary'>Edit</button>")
data += "</tr>"
id = id + 1
End While
End Using
con.Close()
End Using
data += "</table>"
Return data
End Function
Function customSQL(ByVal sqlCommand As String, Optional scalar As Boolean = False) As String
Dim result As String = ""
Try
Dim myConnSTR As String = ConfigurationManager.ConnectionStrings("mainDatabase").ConnectionString
Dim con As New SqlConnection(myConnSTR)
Dim cmd As New SqlCommand(sqlCommand, con)
con.Open()
If scalar = True Then
result = cmd.ExecuteScalar()
Else
result = cmd.ExecuteNonQuery()
result += " - ok"
End If
con.Close()
Catch ex As Exception
result = "Error: " & ex.Message
End Try
Return result
End Function
Public Function selectData(ByVal id As Integer) As String
Dim sqlcommand As String = "SELECT [Questions] FROM [Answers] WHERE [id]=" & id & ";"
Dim result = customSQL(sqlcommand, False)
Return result
End Function
'add the specific column to the html table
Private Function addColumn(ByVal ColumnName As String) As String
Return "<td><div>" & ColumnName & "</div></td>"
End Function
Private Function getIndex() As String
End Function
'deletes all the previous data from the table and inserts the new data
Public Function InsertData(ByVal question As String, ByVal answer As String, ByVal keywords As String) As String
Dim data As String = String.Empty
Dim query As String = String.Empty
query &= "INSERT INTO Answers (Questions, Answers,Keywords) VALUES (@Questions, @Answers, @Keywords)"
Try
Using conn As New SqlConnection("Server=DESKTOP-3J246S1\SQLEXPRESS01;Database=Questions;User Id=kostas;Password=12345rondo;")
Using comm As New SqlCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.AddWithValue("@Questions", question)
.Parameters.AddWithValue("@Answers", answer)
.Parameters.AddWithValue("@Keywords", keywords)
End With
Try
conn.Open()
comm.ExecuteScalar()
Catch ex As SqlException
data = data & "<hr/>" & query & "<hr/>" & ex.ToString
End Try
End Using
End Using
Catch ex As Exception
data = data & "<hr/>" & query & "<hr/>" & ex.ToString
End Try
Return data
End Function
End Class
End Namespace