Vb.net read settings file - json

I created a settings file for my application, like this:
username: ProGamingHun
version: 1.0
maxmemory: 1GB
minmemory: 512MB
I want to read username, so Dim username = in the settings file: ProGamingHun, how can i do this? The username is unknow leght, because ProGamingHun is a test text. Thanks for helping.

I'm not convinced, the code you posted as answer, parses the file correctly. You're better off, using regular expressions instead:
Dim lines() As String = IO.File.ReadAllLines(SettingsRoot + "\config.cfg")
Dim matches = lines.SelectMany(Function(line) Regex.Matches(line, "(.*): (.*)").Cast(Of Match))
Dim dictionary = matches.ToDictionary(Function(match) match.Groups(1).Value, Function(match) match.Groups(2).Value)
dictionary will now contain the key-value pairs of your settings. Even though this wasn't your original question, you can display them in a message box one by one with the following code:
For Each setting In dictionary
MessageBox.Show(setting.Key & "=" & setting.Value)
Next
Nevertheless, I'd suggest you use one of many standard formats for saving settings. This way, you can use existing libraries to parse them:
XML: ConfigurationManager class
JSON: Json.NET
INI: INI File Parser
EDIT
To get values from the dictionary into individual variables, use dictionary.TryGetValue which handles missing keys (settings):
Dim username As String = Nothing
dictionary.TryGetValue("username", username)

Related

How to retrieve a LongBlob from mySQL as a string in UTF-8 in grails?

I'm trying to retrieve an object from a mysql db and one of the attributes in the entity is a String, in the database it is saved as a Longblob.
String zipCode
String streetName
String description
String metadata
String status
String cards
The attribute is cards
When I try to retrieve and "cards" has an accent or special character [à,á,è,ì,ñ, etc] it transforms it to an unreadable character "��".
When I get this same object from a Go app it does not have any problems with accents.
I've seen some similar questions about encoding in grails with the following solutions
Add this string at the end of the connection URL
"?useUnicode=true&characterEncoding=UTF-8"
Change the config of grails with UTF-8
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
But they did not work.
thank you in advance for your help.

How can I search a MYSQL database varchar() table column for existence of ANY of list of "keyword" strings?

I'm building a CRUD application on the Python Flask framework with MYSQL backend and HTML5 front end. I need to parse search text entered by the user into the HTML form text input element and subsequently POST'ed to the server upon clicking the HTML submit input element ("button"). I would like to query the MYSQL database off a python list of string "keywords" derived from splitting the text retrieved from the form POST using the .split() method in python; a list of string "keywords" will result. I know you can search for instances of a single string literal using the LIKE keyword with the (~% OR ~__) functionalities in SQL but I don't think SQL implementations have anything like a list of elements like Python supports.
To summarize, I need to form a dynamic SQL query based on a list of python strings that depends on what a user inputs to an HTML form text input. The list will vary in size --but not be huge -- based on the whims of user input.
I intend to split on white-space a single POST string coming from an HTML form POST space into a list of strings using the SomeString.split() method in Python. and construct a query from that list. Its a bit confusing to me how to do this for a list of arbitrary size. I understand there is a Full-text search feature in MYSQL based on indexing but that exists on the Database side while the list exist on the Python server side.
...
...
def parser(PostedString):
lastword = PostedString[-1]
keywords = PostedString.split()
sqlquery = """SELECT *
FROM vehicle
WHERE VehicleDescr LIKE %"""
for word in keyword[:-1}:
sqlquery = sqlquery + word + "% OR LIKE"
sqlquery = sqlquery + "%" + lastword + "%"
return sqlquery
...
...
cursor.exectute(sqlquery)
result = cursor.fetchall()
I hope to get a subset of the rows in the table. I worry that this method will not be efficient for large datasets in a database. I would like implement the fastest, most comprehensive solution but I am not super advanced in python, flask, or SQL. Thank You

How do I get system.byte to MySQL Bit type?

I have a MySQL database (5.6 Community) with a column of type bit(60). The field holds values such as 1001, 0011, etc. I am trying to pass a string of "1010" to the database through a vb.net adapter. If I use a regular query it would look like this:
insert into my_table (my_bit_field) values (b'1010');
This works and inserts the string exactly as shown but I need to use a data adapter so I can't send the query directly.
When using the data adapter in vb.net, I was getting an error saying that it was expecting a byte array. So I tried using that:
System.Text.Encoding.ASCII.GetBytes("1010")
but it just converted it to its ASCII representation of bytes (49,48,49,48).
Is there a better way to go through the data adapter and is there a way to do this?
Thanks.
In this case, you could try the following to convert your string to a byte array:
Dim bytes() As Byte = { Convert.ToByte("1010", 2) }
However, that breaks once you have more than 8 bits in your string. You could (perhaps should) break the string into byte-sized sections and convert each to a byte, such as for this question. Since you have a bit(60) column you could also cheat a little and use something like this:
Dim inputValue As String = "000100000010000000110000010000000101000001100000011100001000"
If inputValue.Length > 60 Then ' up to 64 could be supported
' Error
End If
Dim longValue As ULong = Convert.ToUInt64(inputValue, 2)
Dim bytes() As Byte = BitConverter.GetBytes(longValue)
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
This will give you the byte array that you can presumably use in your data adapter code that was not shown.

Determining Encoding in MS Access Database

I have a MS Access database with a column that has some strange encoding. Oddly, I am unable to copy/paste this into anything (Chrome, Word, etc), because it strips out most of the unicode characters (though not all of them). What I am wondering, is there a way to determine what type of encoding is being used here?
Somehow the program I am using is taking this column and decoding it to readable text. I converted the Access database to PostgreSQL on a Linux system, but I'm pretty sure whatever encoding is being used here did not map correctly into the PostgreSQL database. What I'm trying to do is to convert this to hex, but I cannot do it since I'm unable to copy/paste the characters out of the database.
You can open the table as a recordset. Then loop the records and convert the field to hex using a function like this:
Public Function StrToByte(ByVal strChars As String) As String
Dim abytChar() As Byte
Dim lngChar As Long
Dim strByte As String
abytChar() = StrConv(strChars, vbFromUnicode)
strByte = Space(2 * (1 + UBound(abytChar) - LBound(abytChar)))
For lngChar = LBound(abytChar) To UBound(abytChar)
Mid(strByte, 1 + 2 * lngChar) = Hex(abytChar(lngChar))
Next
StrToByte = strByte
End Function
Or create a query:
Select *, StrToByte([EncryptedFieldName]) As HexField
From tblYourTable

How to convert Json string with array of objects to a datatable?

Code converts the json string to table. But my problem is, it bypass the Array Chars. So only two fields are printed on the table. It kind of skips the middle field: "Chars"
Dim JsonStr As String = "[{""Name"": ""Banana Pudding"", ""Chars"": [""abc"",""xyz""],""ID"": ""143""}]"
tb = JsonConvert.DeserializeObject(Of DataTable)(JsonStr)
GridView1.DataSource = tb
GridView1.DataBind()
Your database does not support array as a string, actually I do not know any db that do the coerce for a string to array, you have to change the value of Chars to a simple type, break it and it will work.
You may test your code with a binary field instead of string in your table, you did not mention which DBMS are you using, so I can not be more specific. but using a binary field, worth a shot, try it before breaking array to simple types.