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. C#
  4. A C# SQL question

A C# SQL question

Scheduled Pinned Locked Moved C#
questiondatabasecsharp
5 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.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    Hi, When I want to specify an SQL query I am using the below code: mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = 'Brown'"; But how can I do this if I don't want to write down the surname(use a variable instead of that)? I used below code but couldn't get a result: string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '@buffer_surname'"; ??? Thank you, Cem Louis

    T 1 Reply Last reply
    0
    • C cemlouis

      Hi, When I want to specify an SQL query I am using the below code: mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = 'Brown'"; But how can I do this if I don't want to write down the surname(use a variable instead of that)? I used below code but couldn't get a result: string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '@buffer_surname'"; ??? Thank you, Cem Louis

      T Offline
      T Offline
      tdciDoug
      wrote on last edited by
      #2

      string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '" + buffer_surname.ToString() + "'"; You also should probably do a replace and replace any apostrophes with double apostrophes in buffer_surname, just in case. Hope this helps.

      C 1 Reply Last reply
      0
      • T tdciDoug

        string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '" + buffer_surname.ToString() + "'"; You also should probably do a replace and replace any apostrophes with double apostrophes in buffer_surname, just in case. Hope this helps.

        C Offline
        C Offline
        cemlouis
        wrote on last edited by
        #3

        Thank you much...

        T 1 Reply Last reply
        0
        • C cemlouis

          Thank you much...

          T Offline
          T Offline
          tdciDoug
          wrote on last edited by
          #4

          Look a few posts down at Heath's response about an SQL statement. While it is very humorous, he is also VERY correct. You have the same issue.

          C 1 Reply Last reply
          0
          • T tdciDoug

            Look a few posts down at Heath's response about an SQL statement. While it is very humorous, he is also VERY correct. You have the same issue.

            C Offline
            C Offline
            cemlouis
            wrote on last edited by
            #5

            Hi, I used the below code to EMPTY a table which is named people but it is not working???: SqlConnection mySqlConnection4 = new SqlConnection("Initial Catalog=mydb;Data Source=" + comboBox1.SelectedItem.ToString() + ";Integrated Security=SSPI;"); mySqlConnection4.Open(); SqlCommand mySqlCommand4 = mySqlConnection4.CreateCommand(); SqlTransaction myTrans; myTrans = mySqlConnection4.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction"); mySqlCommand4.Connection = mySqlConnection4; mySqlCommand4.Transaction = myTrans; mySqlCommand4.CommandText = "EMPTY people" ; //mySqlCommand4.ExecuteNonQuery(); myTrans.Commit(); mySqlConnection4.Close(); Anyway What is the problem??? Thank you by the way... Cem Louis

            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