-8

Hello everybody hope you are well.

I have a place on a project im working on that im struggling to get past. I'm basically gathering meta data of a song and assigning them to variables in VB.

I'm next trying to save this data to a SQL database with tables i have created in visual studio and im having trouble doing this. The problem lies within trying to link the VB variables into a SQL query. Any Ideas?

Lupe_99
  • 31
  • 1
  • 6
  • 2
    We would need to see the code to have any ideas on what can go wrong. – the_lotus Jan 07 '19 at 15:49
  • Read up on ADO.NET, which is the standard .NET data access technology. There's loads of information around on the subject. If you have a specific issue then we need specific details of that issue. – jmcilhinney Jan 07 '19 at 16:13

1 Answers1

-1

To link VB variables to an SQL query, you need to add parameters that store the variables and then use those parameters in your SQL command:

Using parameters

MyCommand = New SqlCommand("SELECT Height, Weight, DoB, Gender, FROM `User` WHERE Username = @Username", DatabaseConnection)

MyCommand.Parameters.AddWithValue("@Username", Username)

UPDATE: How to connect to database

'Leads to the database
Public Filename As String = $"{AppDomain.CurrentDomain.BaseDirectory}YourDatabase.accdb"
'The connection string is set
Public ConStr As String = $"PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA Source = {Filename}"
'The connection to the database is defined
Public DatabaseConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConStr)
Mr Popo
  • 362
  • 5
  • 20
  • 3
    NO! Do not concatenate strings to build SQL queries. Use parameters or risk SQL injection. – Mary Jan 07 '19 at 18:54
  • Thank you, you no longer need the $ for an interpolated string. I will delete my comments in a few minutes. – Mary Jan 07 '19 at 19:01
  • 1
    "Not advised" is [quite an understatement](https://stackoverflow.com/q/332365/11683). Please remove that section entirely. – GSerg Jan 07 '19 at 19:04
  • What do you mean by "Databaseconnection", is that the dataset connected to my database? or a table adapter. Either way im struggling to get this to work. – Lupe_99 Jan 09 '19 at 14:06
  • @Lupe_99 Your question was not related to connecting to the database but putting VB variables into SQL. I will update my answer to include your update – Mr Popo Jan 09 '19 at 17:10
  • Returns this error- Error 1 Value of type 'System.Data.OleDb.OleDbConnection' cannot be converted to 'System.Data.SqlClient.SqlConnection'. – Lupe_99 Jan 14 '19 at 14:12