Simple SQL INSERT

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a SQL INSERT statement. I'm pretty well-versed in SQL but NOT in
VB.NET / ASP.NET. I would like to know what VB code is for inserting a new
row into a database. I have a SQL Table established already. I would like
to know the code required to insert a new row to that SQL Table. I don't
have any connection strings established yet, I don't have anything. Can you
please take pity on a poor .NET Newb and tell me what I should be doing?
I've tried several examples and have yet to successfully insert a record. I
think I need to know how to establish the connection, and then write (insert)
a new record to that connection.

Thanks in advance. This stuff can be very overwhelming.
 
Scott,

You only need a SQLClient.Connection,
a SQLClient.SQLCommand
and from the last an executenonquery

As pseudo sample
\\\
dim conn as new SQLClient.SQLConnection(ConnectionString)
dim cmd as new SQLClient.SQLCommand("YourSQLTransactInsertString",conn)
cmd.ExecuteNonQuery()
///

Have a look here for the connectionstring
http://www.connectionstrings.com/

I hope this helps,

Cor
 
And let's not forget the parameters, how i do this in OLEDB:

cmd.parameters.addwithvalue("", "hello" )
For date extend:
cmd.parameters.addwithvalue("", d.Tooadate() )

Then the INSERT or SELECT should contain ? for the field values like WHERE
ID = ? and so on.
 
Edwin,

In SQLClient this is


cmd.parameters.addwithvalue("@ToInsert", "hello" )
For date extend:
cmd.parameters.addwithvalue("@TheWhere", d.Tooadate() )

Then the INSERT or SELECT should contain @ToInsert for the field values like
WHERE
ID = @TheWhere and so on.


Cor
 
Hmm, it differs :)

At some point we will start using sqlserver as well.

Is this syntax compatible with OleDB?
Can test that of course :)

Thanks,
 
Back
Top