SQL Server From .Net: Making A Connection

  • timwell - Tuesday, April 17, 2018 8:05 AM

    thierry.vandurme - Tuesday, April 17, 2018 6:18 AM

    Thanks for sharing.
    I may be wrong but I don't think any of the SqlConnection constructors throws exceptions (cfr https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.sqlconnection(v=vs.110).aspx)
    So the try/catch shouldn't be necessary when creating it, only when opening the connection

    By supplying a really incorrect connection string I was able to cause an ArgumentException exception. That is why I included the try/catch for that.
    It's not thrown by the constructor itself but when it's parsing the string. 
    If your formatting is good enough adding a try/catch for that might be more than necessary.

    Cool, thanks for the feedback!

  • I get a red squiggly line under this statement:
    using System.Data.SqlClient;
    the hint says "the type or namespace SqlClient does not exist in the namespace System.Data
    Tried adding references to System.Data.dll (I found a ton of them on my machine), but it doesn't seem to help

  • Misha_SQL
    You might need to add System.Data to the project references. When you are typing it intellisence should show you the available items.

  • Misha_SQL - Friday, April 20, 2018 9:15 AM

    I get a red squiggly line under this statement:
    using System.Data.SqlClient;
    the hint says "the type or namespace SqlClient does not exist in the namespace System.Data
    Tried adding references to System.Data.dll (I found a ton of them on my machine), but it doesn't seem to help

    Jonathan AC Roberts - Friday, April 20, 2018 10:03 AM

    Misha_SQL
    You might need to add System.Data to the project references. When you are typing it intellisence should show you the available items.

    Jonathan is correct, what I have in my project references is System.Data, with the same version numbers as the System reference.
    When you said you tried adding references to System.Data.dll was that in the project references (right-click references and select Add Reference) or something else?

  • timwell - Friday, April 20, 2018 10:56 AM

    Misha_SQL - Friday, April 20, 2018 9:15 AM

    I get a red squiggly line under this statement:
    using System.Data.SqlClient;
    the hint says "the type or namespace SqlClient does not exist in the namespace System.Data
    Tried adding references to System.Data.dll (I found a ton of them on my machine), but it doesn't seem to help

    Jonathan AC Roberts - Friday, April 20, 2018 10:03 AM

    Misha_SQL
    You might need to add System.Data to the project references. When you are typing it intellisence should show you the available items.

    Jonathan is correct, what I have in my project references is System.Data, with the same version numbers as the System reference.
    When you said you tried adding references to System.Data.dll was that in the project references (right-click references and select Add Reference) or something else?

    Thank you everybody. I think my problem was that I created a wrong type of application, so none of the default references were included. After re-creating as Console App (.NET Framework), as a the article mentions, I see a bunch of references specified including System.Data, so it's all good now.  Sorry, I don't do much C#.

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply