Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Writing to SQL server w/ VB.Net

Writing to SQL server w/ VB.Net

Scheduled Pinned Locked Moved Visual Basic
databasecsharpsql-serversysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    svanwass
    wrote on last edited by
    #1

    First the setup My Code: Imports System.Data.SqlClient Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myConnection As New SqlConnection() Dim myDataSet As New DataSet() Dim myDataAdapter As SqlDataAdapter Dim myUpdateCommand As SqlCommand Dim myNewStr As String 'create connection string myConnection.ConnectionString ="Server=.;Database=TestDB;UID=steve;pwd=password;Trusted_Connection=yes;" 'open connection to DB myConnection.Open() 'setup data adapter myDataAdapter = New SqlDataAdapter("select TEXT from SCRIPTS WHERE ID=1", myConnection) 'Initialize the SqlCommand object that will be used as the DataAdapter's UpdateCommand. 'Note that the WHERE clause uses only the CustId field to locate the record that is to be updated. myUpdateCommand = New SqlCommand("UPDATE SCRIPTS SET TEXT=@pNewText FROM SCRIPTS WHERE ID =@pMyID", myDataAdapter.SelectCommand.Connection) 'Create and append the parameters for the Update command. myUpdateCommand.Parameters.Add(New SqlParameter("@pNewText", SqlDbType.VarChar)) myUpdateCommand.Parameters("@pNewText").SourceVersion = DataRowVersion.Current myUpdateCommand.Parameters("@pNewText").SourceColumn = "TEXT" myUpdateCommand.Parameters.Add(New SqlParameter("@pMyID", SqlDbType.Int)) myUpdateCommand.Parameters("@pMyID").SourceVersion = DataRowVersion.Original myUpdateCommand.Parameters("@pMyID").SourceColumn = "ID" 'Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter. myDataAdapter.UpdateCommand = myUpdateCommand 'call the fill method to populate our dataset myDataAdapter.Fill(myDataSet, "SCRIPTS") 'present the current entry in database MsgBox("Script before updating" + vbCrLf + Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT"))) 'modify current with replace to update field myNewStr = Replace(Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT")), "hello", "steve") 'now convert our replaced field to a byte array Dim dBytes As Byte() dBytes = StrToByteArray(myNewStr) 'set the dataset table row to our converted byte array myDataSet.Tables("SCRIPTS").Rows(0)("TEXT") = dBytes 'c

    D 1 Reply Last reply
    0
    • S svanwass

      First the setup My Code: Imports System.Data.SqlClient Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myConnection As New SqlConnection() Dim myDataSet As New DataSet() Dim myDataAdapter As SqlDataAdapter Dim myUpdateCommand As SqlCommand Dim myNewStr As String 'create connection string myConnection.ConnectionString ="Server=.;Database=TestDB;UID=steve;pwd=password;Trusted_Connection=yes;" 'open connection to DB myConnection.Open() 'setup data adapter myDataAdapter = New SqlDataAdapter("select TEXT from SCRIPTS WHERE ID=1", myConnection) 'Initialize the SqlCommand object that will be used as the DataAdapter's UpdateCommand. 'Note that the WHERE clause uses only the CustId field to locate the record that is to be updated. myUpdateCommand = New SqlCommand("UPDATE SCRIPTS SET TEXT=@pNewText FROM SCRIPTS WHERE ID =@pMyID", myDataAdapter.SelectCommand.Connection) 'Create and append the parameters for the Update command. myUpdateCommand.Parameters.Add(New SqlParameter("@pNewText", SqlDbType.VarChar)) myUpdateCommand.Parameters("@pNewText").SourceVersion = DataRowVersion.Current myUpdateCommand.Parameters("@pNewText").SourceColumn = "TEXT" myUpdateCommand.Parameters.Add(New SqlParameter("@pMyID", SqlDbType.Int)) myUpdateCommand.Parameters("@pMyID").SourceVersion = DataRowVersion.Original myUpdateCommand.Parameters("@pMyID").SourceColumn = "ID" 'Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter. myDataAdapter.UpdateCommand = myUpdateCommand 'call the fill method to populate our dataset myDataAdapter.Fill(myDataSet, "SCRIPTS") 'present the current entry in database MsgBox("Script before updating" + vbCrLf + Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT"))) 'modify current with replace to update field myNewStr = Replace(Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT")), "hello", "steve") 'now convert our replaced field to a byte array Dim dBytes As Byte() dBytes = StrToByteArray(myNewStr) 'set the dataset table row to our converted byte array myDataSet.Tables("SCRIPTS").Rows(0)("TEXT") = dBytes 'c

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      svanwass wrote:

      'Create and append the parameters for the Update command. myUpdateCommand.Parameters.Add(New SqlParameter("@pNewText", SqlDbType.VarChar))

      You created this parameter as a string, not as a binary type. This is where the error is comming from.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups