Help in using queries from MS Access in Visual C# .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itsme7
    New Member
    • Sep 2007
    • 9

    Help in using queries from MS Access in Visual C# .NET

    Hello,
    I'm working on a little project that has to get into a database on a server. My question is: How am I supposed to use the queries I made in MS Access in my C# project? I'm talking about queries such as:

    Code:
    SELECT Count(*) AS IDExists
    FROM tblCustomer
    WHERE ID=[@ID];

    How am I supposed to send parameters (such as "@ID") from my C# code into the query?
    I mean, if I want to use the query as a function and put it in an if, for example: if(storedProced ureCount>0)
    Please help, I'm a beginner as you see, I will appreciate it if you explain me in detail.
    With thanks, itsme7
  • hbxtlhx
    New Member
    • Sep 2007
    • 10

    #2
    Originally posted by itsme7
    Hello,
    I'm working on a little project that has to get into a database on a server. My question is: How am I supposed to use the queries I made in MS Access in my C# project? I'm talking about queries such as:

    Code:
    SELECT Count(*) AS IDExists
    FROM tblCustomer
    WHERE ID=[@ID];

    How am I supposed to send parameters (such as "@ID") from my C# code into the query?
    I mean, if I want to use the query as a function and put it in an if, for example: if(storedProced ureCount>0)
    Please help, I'm a beginner as you see, I will appreciate it if you explain me in detail.
    With thanks, itsme7
    such as:

    string queryString = "SELECT Count(1) AS IDExists FROM tblCustomer HERE ID=?";
    OleDbCommand command = new OleDbCommand(qu eryString, connection);
    command.Paramet ers.Add("@p1", OleDbType.Char, 3).Value = "a";
    object retValue= command.Execute Scalar();
    if (retValue is int && (int)retValue>0 )
    {
    //exists records
    }
    else
    {
    //not exists
    }

    Comment

    • itsme7
      New Member
      • Sep 2007
      • 9

      #3
      Originally posted by hbxtlhx
      such as:

      string queryString = "SELECT Count(1) AS IDExists FROM tblCustomer HERE ID=?";
      OleDbCommand command = new OleDbCommand(qu eryString, connection);
      command.Paramet ers.Add("@p1", OleDbType.Char, 3).Value = "a";
      object retValue= command.Execute Scalar();
      if (retValue is int && (int)retValue>0 )
      {
      //exists records
      }
      else
      {
      //not exists
      }
      Mmm OK, just a couple questions:
      1. Why did you change the * in count to 1?
      2. How do I manage the connections, I mean, what am I supposed to write in the code in order to make a connection?
      3. Is the "a" a parameter (I mean, is it the @ID)?
      4. What does the parameter OleDbType.Char and 3 mean?
      5. What if I have more than 1 parameter? like in the following query:
      Code:
       SELECT WasUsed
      FROM tblCustomer
      WHERE CPUCode=[@CPUCode];
      6. Why did you put a question mark instead of [@ID]?

      Oh and another thing: the Value in line command.Paramet ers.Add("@p1", OleDbType.Char, 3).Value = "a"; doesn't pop up when I write the code. I am using Visual Studios .NET 2003.

      Comment

      Working...