Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepared Statements in VB.NET

I am new to prepared statements in vb.net and Microsoft SQL Server 2008. I can't really find any good sources for connecting to a database via connection string and executing prepared statements. Could someone show me an example or point me to a resource that might be useful?

like image 588
user489041 Avatar asked Sep 08 '11 16:09

user489041


People also ask

What is in prepared statement?

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").

Why are prepared statements used?

Overview of Prepared StatementsIf you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead. The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created.

What is the difference between a prepared statement and a statement?

Statement – Used to execute string-based SQL queries. PreparedStatement – Used to execute parameterized SQL queries.

What is prepared statement in stored procedure?

Prepared Statement. Stored procedures are a sequence of SQL statements that access the relational database management system. Prepared statements are queries that contain the placeholders instead of actual values. It can be stored in the database server.


2 Answers

Here's some quick example code:

Using cn  As New SqlConnection("your connection string here"), _
      cmd AS New SqlCommand("SELECT * FROM Table WHERE ID= @ID", cn)

    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 12345

    cn.Open()
    Using rdr As SqlDataREader = cmd.ExecuteReader()
        While rdr.Read()
            'Do something with the record
        End While
        rdr.Close()
    End Using
End Using

Of course you need to Import System.Data and System.Data.SqlClient.

like image 137
Joel Coehoorn Avatar answered Sep 19 '22 05:09

Joel Coehoorn


Prepared statements are nothing but Parametrized SqlCommands enclosed in a Transaction.

For example, this is a Prepared Statement:

Using c As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
   c.Open()
using mytransaction = c.BeginTransaction()

   Dim command = New SqlCommand("INSERT INTO yourtable(image) values (@image)", c)
   ''# this is specific to the FileUploadControl but the idea is to get the
   ''#image in a byte array; however you do it, it doesn't matter
    Dim buffer(FileUpload1.PostedFile.ContentLength) As Byte
    FileUpload1.PostedFile.InputStream.Read(buffer, 0, buffer.Length)
    command.Parameters.AddWithValue("@image", buffer)
    command.ExecuteNonQuery()    
 mytransaction .Commit()
End Using
End Using
like image 44
Icarus Avatar answered Sep 22 '22 05:09

Icarus