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. Database & SysAdmin
  3. Database
  4. my problem with ado.net

my problem with ado.net

Scheduled Pinned Locked Moved Database
csharpdatabasehelpquestionphp
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.
  • D Offline
    D Offline
    da vinci coder
    wrote on last edited by
    #1

    hi guys.. my problem is like this.. i have a query which is : string query = "select count(*) AS [username] from [" + tablename + "] WHERE username = '"+username+"'"; and my method is : public void checkIfUserExists(string username,string tablename) as u can see, i want to check how many user exists with this username in the database, i want to execute the query, and take the result inside an int ( e.g. int result ) if the result is > 0 , then i won`t allow user to choose that username else user can register.. how can i do this ? it was easy doing that at php but i cant do this in c# because i can`t understand ado.net very well ( datasets, datagrids,etc..) any help would be great! thx! good coding !

    C 1 Reply Last reply
    0
    • D da vinci coder

      hi guys.. my problem is like this.. i have a query which is : string query = "select count(*) AS [username] from [" + tablename + "] WHERE username = '"+username+"'"; and my method is : public void checkIfUserExists(string username,string tablename) as u can see, i want to check how many user exists with this username in the database, i want to execute the query, and take the result inside an int ( e.g. int result ) if the result is > 0 , then i won`t allow user to choose that username else user can register.. how can i do this ? it was easy doing that at php but i cant do this in c# because i can`t understand ado.net very well ( datasets, datagrids,etc..) any help would be great! thx! good coding !

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      The way you build your SQL string is susceptable to a SQL Injection attack you may wish to read SQL Injection attacks and tips on how to prevent them[^] You don't say what database you are using so I'll assume SQL Server 2000

      SqlConnection conn = new SqlConnection(connectionString);
      SqlCommand cmd = new SqlCommand(query, conn);
      conn.Open();
      int result = (int)cmd.ExecuteScalar();
      conn.Close();

      This is a very basic example and does not take into account error conditions. You have to provide the connection string (as I know nothing about your database or the security you've set up) and the query (which you have above - although I do recommend securing it as shown in the article I've linked to) Does this help?


      My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

      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