C# continuation lines


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: C# continuation lines

  1. #1
    Join Date
    Dec 2008
    Posts
    46

    C# continuation lines

    Hi:
    I'm just wondering about something odd here. I thought that in C#, you didn't have to use any special character to continue a line of code on a new line as you do with VB which requires something like "& _" if memory serves.

    But in this little sample I'm doing, as long as the line from the con.ConnectionString = (on the right side of the "=" )
    is all on one long continuous line, the program will run. But if I try to split it up in two lines, as it is below:

    "Data Source=.\\SQLEXPRESS;AttachDbFilename=
    C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    then I get errors . It doesn't seem to matter where I try to continue the line.

    Just wondering what's up with that??

    Rachel

    Here is the snippet:

    System.Data.SqlClient.SqlConnection con;
    private void Form1_Load(object sender, EventArgs e)
    {
    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString =
    "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    con.Open();
    MessageBox.Show("Database Open");
    con.Close();
    MessageBox.Show("Databased Closed");

    }

    Rachel
    Last edited by RachelKC; 01-04-2009 at 12:43 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You may split a line of C# code wherever you wish, because the compiler scans until it finds a semicolon (;) to indicate the end of the line. But you may not split a string literal (text between quotation marks) in the middle. In order to split a string literal, you must use concatenation:
    Code:
    con.ConnectionString = 
        "Data Source=.\\SQLEXPRESS;" + 
        "AttachDbFilename=C:\\MyWorkers.mdf;" + 
        "Integrated Security=True;" + 
        "Connect Timeout=30;" + 
        "User Instance=True";
    P.S. -- Wrong forum again. ;-)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2008
    Posts
    46
    Quote Originally Posted by Phil Weber View Post
    You may split a line of C# code wherever you wish, because the compiler scans until it finds a semicolon ( to indicate the end of the line. But you may not split a string literal (text between quotation marks) in the middle. In order to split a string literal, you must use concatenation:
    Code:
    con.ConnectionString = 
        "Data Source=.\\SQLEXPRESS;" + 
        "AttachDbFilename=C:\\MyWorkers.mdf;" + 
        "Integrated Security=True;" + 
        "Connect Timeout=30;" + 
        "User Instance=True";
    P.S. -- Wrong forum again. ;-)
    Thanks. I think I read that but had forgotten! (That's easier than VBs)

    and Dangit!! about getting the wrong forum again!
    Let me guess... it should have been under database??
    I'm so confused about what all is "ASP.NET" .... since it seems you can do everything in it. Maybe it'll all make sense one day.
    (And I didn't see a forum for C#)
    Rachel

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    No, just the .NET forum (I've already moved it). ASP.NET is specifically about Web applications that run in a browser. If your question is not about Web Forms or Web controls or postbacks, just post it to the .NET forum. Thanks!
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Jan 2009
    Posts
    2
    If you want it to look a little cleaner, try the following. Not sure if the ConnectionString parameter accepts \n's in it.

    Code:
    con.ConnectionString = 
        @"Data Source=.\SQLEXPRESS;
        AttachDbFilename=C:\MyWorkers.mdf;
        Integrated Security=True;
        Connect Timeout=30;
        User Instance=True";

  6. #6
    Join Date
    Jan 2009
    Posts
    5
    Just an FYI, you really shouldn't be hard coding your connection strings directly into your applications. Use the app/web config file connectionStrings section for that. You'll need to add a reference to System.Configuration assembly to gain access to the ConfigurationManager class as well.

    Here's an example of what your config file might look like, and how to make use of it:

    Code:
    <configuration>
      <connectionStrings>
        <add name="MyConnString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    </configuration>
    
    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
    That will allow you to change your connection strings without requiring a build of your application. If security is a concern .NET does provide encryption capabilities (depending on what you need) for sections of your configuration file.

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2010, 04:17 AM
  2. Lines with Text in Excel
    By svn in forum VB Classic
    Replies: 0
    Last Post: 03-05-2007, 11:23 AM
  3. Space between lines in MS Access Report
    By rpatil in forum Database
    Replies: 5
    Last Post: 12-02-2006, 05:10 PM
  4. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  5. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center