vb.net - Attempting to create code in Visual Basic to connect to a specific table in SQL Server, where to start? -
imports system.data.sqlclient imports system.data public class form1 inherits system.windows.forms.form 'create ado.net objects. private myconn sqlconnection private mycmd sqlcommand private myreader sqldatareader private results string private sub form1_load(sender object, e eventargs) handles mybase.load 'create connection object. myconn = new sqlconnection("initial catalog=contacts;" & "data source=) 'create command object. mycmd = myconn.createcommand mycmd.commandtext = "select " end sub end class
that have far , getting info google. first project in vb. boss man wants me become programmer. unsure how initial catalog , data source bit written.
i'm no expert on this, try this:
imports system.data.sqlclient public class form1 private sub form1_load(sender object, e eventargs) handles mybase.load dim ssqlcommand string = "select yourcolumnname yourtablename" dim myconnection new sqlconnection("data source=127.0.0.1;initial catalog=yourdatabasename;user id=yourusername;password=yourpassword;") dim mycommand new sqlcommand(ssqlcommand, myconnection) dim mydataadapter new sqldataadapter(mycommand) dim mydataset new dataset dim mydatatable new datatable try myconnection.open() mydataadapter.fill(mydataset, "yourtablename") if mydataset isnot nothing andalso mydataset.tables.count > 0 andalso mydataset.tables(0).rows.count > 0 mydatatable = mydataset.tables(0) end if catch ex exception ' handle error here if not isnothing(myconnection) if myconnection.state <> connectionstate.closed myconnection.close() end if end if end try end sub end class
you'll want substitute sql statement, ip address of sql instance (if it's on local machine, 127.0.0.1 should work), database name, username , password (which shouldn't hardcoded in production) , table name.
i'm sure there better resources this, think work. of information coming sql server management studio.
Comments
Post a Comment