VB.net - SQL Add/Retieve records.

Soldato
Joined
4 Nov 2007
Posts
4,514
Can anyone give me an 'overview' of how I can go about this? Googling my hands off the past week and can't find a definitive way of doing this.

All I've managed to do so far if this, which connects to the database fine.

No idea how to enter a record, I assumed you could just do an SQL statement and fill it from objects, but have no idea how to 'format' the argument (" " etc) or how to actually go about entering it once the argument has been formatted.



Code:
Dim sqlConn As New SqlConnection("Data Source=hbcsql3;Initial Catalog=mailreader;User Id=usernamegoeshere;Password=pwgoeshere;")
Dim sqlAdap As New SqlDataAdapter("SELECT * FROM mail_info", sqlConn)
Dim sqlRS As String
Dim ContractStartPath As String
Dim ContractPath As String
Dim DatePath As String
Dim ID As String
Dim AllPath As String
Dim LengthPath As Integer
Dim DateLengthPath As Integer
Dim DatePathExe As String

        AllPath = Application.ExecutablePath
        LengthPath = AllPath.Length - 9
        
        ContractStartPath = AllPath.Remove(0, 3)
        ContractPath = ContractStartPath.Remove(6, LengthPath)

        ID = DateTime.Now & DateTime.Now.Millisecond

        DateLengthPath = AllPath.Length - 17
        DatePathExe = AllPath.Remove(0, DateLengthPath)
        DatePath = DatePathExe.Remove(8, 9)

[B]sqlRS = "INSERT into mail_info(name, mail_date, contract, read_date, id) VALUES (textbox2.Text, DatePath, ContractPath, DateTime.Now.Date, ID)"[/B]

Edit, mod please fix title. Thanks
 
Something like this (untested):

Code:
Dim connection As New SqlConnection("Data Source=hbcsql3;Initial Catalog=mailreader;User Id=usernamegoeshere;Password=pwgoeshere;")
 
connection.Open()
 
Dim sql As String = "INSERT into mail_info(name, mail_date, contract, read_date, id) VALUES (@name, @mailDate, @contract, @readDate, @id)"
 
Dim command As New SqlCommand(sql, connection)
 
[COLOR=yellowgreen]// Get your data from wherever.[/COLOR]
 
command.Parameters.AddWithValue("@name", TextBox2.Text)
command.Parameters.AddWithValue("@mailDate", DatePath)
command.Parameters.AddWithValue("@contract", ContractPath)
command.Parameters.AddWithValue("@readDate", DateTime.Now.Date)
command.Parameters.AddWithValue("@id", ID)
 
command.ExecuteNonQuery()

Usual warranty applies i.e. none whatsoever.
 
Last edited:
Code:
Dim connection As New SqlConnection("Data Source=hbcsql3;Initial Catalog=mailreader;User Id=usernamegoeshere;Password=pwgoeshere;")
 
connection.Open()
 
Back
Top Bottom