Sql string in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shek124
    New Member
    • Oct 2007
    • 50

    Sql string in C#

    HAi ,

    IN C#.NET , i wtite a query in string type variable SQL . But the + or & operator is not supported in string values .. this is my exception while starts the project.

    In vb.net , it is working well .. whats the error. im the newbie to C#.net
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by shek124
    HAi ,

    IN C#.NET , i wtite a query in string type variable SQL . But the + or & operator is not supported in string values .. this is my exception while starts the project.

    In vb.net , it is working well .. whats the error. im the newbie to C#.net
    please give us the code part....

    Comment

    • shek124
      New Member
      • Oct 2007
      • 50

      #3
      Query:
      Code:
      string query = "insert into print_details (Printed_date, Printed_time, User_login, Printer_name, IP_address, Document_name, Total_pages) values('+ date() +', '+ time() +' , '" + m_owner + "', '" + m_Drivername + "', '" + ip_address.ToString + "', '" + m_Documentname + "','" + m_TotalPages + "' )";

      ERROR:
      [HTML]Error2 Operator '+' cannot be applied to operands of type 'string' and 'method group'[/HTML]

      Comment

      • wassssup
        New Member
        • Oct 2007
        • 47

        #4
        Originally posted by shek124
        Query:
        Code:
        string query = "insert into print_details (Printed_date, Printed_time, User_login, Printer_name, IP_address, Document_name, Total_pages) values('+ date() +', '+ time() +' , '" + m_owner + "', '" + m_Drivername + "', '" + ip_address.ToString + "', '" + m_Documentname + "','" + m_TotalPages + "' )";
        [/HTML]
        in C# u should write like this:
        string query= "INSERT INTO print_details" + "([database field name]) "+ " VALUES (@variable)";

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by shek124
          Query:
          Code:
          string query = "insert into print_details (Printed_date, Printed_time, User_login, Printer_name, IP_address, Document_name, Total_pages) values('+ date() +', '+ time() +' , '" + m_owner + "', '" + m_Drivername + "', '" + ip_address.ToString + "', '" + m_Documentname + "','" + m_TotalPages + "' )";

          ERROR:
          [HTML]Error2 Operator '+' cannot be applied to operands of type 'string' and 'method group'[/HTML]
          You are missing some () after ToString. There might also be problems with date()/time() as I don't know how you are using the SQL functions
          Code:
          string query = "insert into print_details (Printed_date, Printed_time, User_login, Printer_name, IP_address, Document_name, Total_pages)";
          query+="values('+ date() +"', '"+ time() +"' , '" + m_owner + "', '" + m_Drivername + "', '" + ip_address.ToString() + "', '" + m_Documentname + "','" + m_TotalPages + "' )";

          Comment

          Working...