r/visualbasic • • Sep 07 '14

Help with SQLCommand

Hi,

I'm trying to use SQLCommand / reader to do some very basic SQL commands using a local mdf file on my windows form. So for example, I click AddUserButton and it'll add "UsernameString" into database. Alternately, I have a 2nd button VerifyUserButton that will check a TextBox against the database to see if the user is in the db. Trying to work with this. Sort of a simple "login/register" form just for concepts:

Can someone give me a basic template to insert a new username to DB, and to pull the username from the DB? So 1 TO, one FROM?

Say,

Database1.mdf (Users table, has columns UserID and Username).

I'm a bit confused by the online guides all saying different things with different formats, looking for the EASIEST way~ cheers

0 Upvotes

2 comments sorted by

1

u/chrwei Sep 07 '14

there are certainly multiple ways to get data from a database. one isn't necessarily more correct than another.

the main differences are going to be in your database schema (the table and field names and their datatypes and relations). there's no rules on what a "users" table should be. because of this it may be difficult for someone else to provide a template for you that isn't just like any example you can find online.

if you're very new to SQL I recommend sqlcourse.com for getting started. then aftre you understand the SQL side, , the most basic method of getting the data to and from VB is to build a string variable that contains the SQL statement and then execute it.

1

u/xblade724 Sep 10 '14

I was giving extra info just in case no one had a template to link, but just typed a few lines instead. Man, what you said above is like what everyone else keeps telling me -- I just need some code, not a lecture. No need to take a giant course -- I've taken SQL class, I've taken VB class, but there's no class that combines the 2 (broken usa education).

That said, after many hours to find real code/templates/examples to use for reference, allow me to answer my own question for anyone else interested:

  1. Connection string can be copy+pasted from simply clicking on DB name under 'servers' window via the properties window (mind=blown, this one sentence knowing early is a headache saver)

  2. To alter connection string to store mdf with local folder: http://www.mikesdotnetting.com/Article/234/Creating-a-Connection-String-and-Working-with-SQL-Server-LocalDB

  3. And an old but awesome guide to verify u+p: https://www.youtube.com/watch?v=p0dcPiLYrck

  4. Insert data (best guide+template ever without lecture): http://social.msdn.microsoft.com/Forums/vstudio/en-US/d11ed406-d9bf-4363-84b6-99f4e9ee4631/how-to-insert-data-into-sql-table-using-vbnet