Connection to SQL database on local computer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • axel22@gmail.com

    Connection to SQL database on local computer

    I have added a new SQL database into my project, added a table manually
    in VS IDE,
    and then I've run the following code that always results in an
    exception?
    Could it be that the connection string is wrong?


    using System;
    using System.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Data.Sql Client;

    namespace ConsoleApplicat ion1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string connectionStrin g =
    @"Data
    Source=.\SQLEXP RESS;AttachDbFi lename=F:\c#\Pr ojects\ConsoleA pplication1"
    +
    @"\ConsoleAppli cation1\Databas e.mdf;Integrate d
    Security=True;U ser Instance=True";
    SqlConnection conn = new SqlConnection(c onnectionString );
    SqlCommand com = new SqlCommand("SEL ECT * FROM tabela1",
    conn);
    SqlDataReader reader = null;
    try
    {
    reader = com.ExecuteRead er();
    }
    catch (InvalidOperati onException)
    {
    System.Console. WriteLine("SQL connection not
    established.");
    reader = null;
    }

  • Otis Mukinfus

    #2
    Re: Connection to SQL database on local computer

    On 4 Nov 2006 03:44:14 -0800, axel22@gmail.co m wrote:
    >I have added a new SQL database into my project, added a table manually
    >in VS IDE,
    >and then I've run the following code that always results in an
    >exception?
    >Could it be that the connection string is wrong?
    >
    >
    >using System;
    >using System.Collecti ons;
    >using System.Collecti ons.Generic;
    >using System.Text;
    >using System.Data.Sql Client;
    >
    >namespace ConsoleApplicat ion1
    >{
    class Program
    {
    static void Main(string[] args)
    {
    string connectionStrin g =
    @"Data
    >Source=.\SQLEX PRESS;AttachDbF ilename=F:\c#\P rojects\Console Application1"
    >+
    @"\ConsoleAppli cation1\Databas e.mdf;Integrate d
    >Security=True; User Instance=True";
    SqlConnection conn = new SqlConnection(c onnectionString );
    SqlCommand com = new SqlCommand("SEL ECT * FROM tabela1",
    >conn);
    SqlDataReader reader = null;
    try
    {
    reader = com.ExecuteRead er();
    }
    catch (InvalidOperati onException)
    {
    System.Console. WriteLine("SQL connection not
    >established.") ;
    reader = null;
    }
    What does the exception say? You need to tell us what the exception was that
    was thrown....
    Good luck with your project,

    Otis Mukinfus


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Connection to SQL database on local computer

      Axel,

      Can you try it with that to detach file on your local system first. It can
      be that you have to give special access permissions on the Server. This
      seems to me as a known subject.

      Cor

      <axel22@gmail.c omschreef in bericht
      news:1162640654 .427758.161190@ m73g2000cwd.goo glegroups.com.. .
      >I have added a new SQL database into my project, added a table manually
      in VS IDE,
      and then I've run the following code that always results in an
      exception?
      Could it be that the connection string is wrong?
      >
      >
      using System;
      using System.Collecti ons;
      using System.Collecti ons.Generic;
      using System.Text;
      using System.Data.Sql Client;
      >
      namespace ConsoleApplicat ion1
      {
      class Program
      {
      static void Main(string[] args)
      {
      string connectionStrin g =
      @"Data
      Source=.\SQLEXP RESS;AttachDbFi lename=F:\c#\Pr ojects\ConsoleA pplication1"
      +
      @"\ConsoleAppli cation1\Databas e.mdf;Integrate d
      Security=True;U ser Instance=True";
      SqlConnection conn = new SqlConnection(c onnectionString );
      SqlCommand com = new SqlCommand("SEL ECT * FROM tabela1",
      conn);
      SqlDataReader reader = null;
      try
      {
      reader = com.ExecuteRead er();
      }
      catch (InvalidOperati onException)
      {
      System.Console. WriteLine("SQL connection not
      established.");
      reader = null;
      }
      >

      Comment

      • Otis Mukinfus

        #4
        Re: Connection to SQL database on local computer

        On 4 Nov 2006 03:44:14 -0800, axel22@gmail.co m wrote:
        >I have added a new SQL database into my project, added a table manually
        >in VS IDE,
        >and then I've run the following code that always results in an
        >exception?
        >Could it be that the connection string is wrong?
        >
        >
        >using System;
        >using System.Collecti ons;
        >using System.Collecti ons.Generic;
        >using System.Text;
        >using System.Data.Sql Client;
        >
        >namespace ConsoleApplicat ion1
        >{
        class Program
        {
        static void Main(string[] args)
        {
        string connectionStrin g =
        @"Data
        >Source=.\SQLEX PRESS;AttachDbF ilename=F:\c#\P rojects\Console Application1"
        >+
        @"\ConsoleAppli cation1\Databas e.mdf;Integrate d
        >Security=True; User Instance=True";
        SqlConnection conn = new SqlConnection(c onnectionString );
        SqlCommand com = new SqlCommand("SEL ECT * FROM tabela1",
        >conn);
        SqlDataReader reader = null;
        try
        {
        reader = com.ExecuteRead er();
        }
        catch (InvalidOperati onException)
        {
        System.Console. WriteLine("SQL connection not
        >established.") ;
        reader = null;
        }
        I'll bet your exception say's something like "must have an open connection".

        You have to open the connection before you call the ExecuteReader method.
        Good luck with your project,

        Otis Mukinfus


        Comment

        • axel22@gmail.com

          #5
          Re: Connection to SQL database on local computer

          Otis: This is the exception I got:

          An unhandled exception of type 'System.Invalid OperationExcept ion'
          occurred in System.Data.dll

          Additional information: ExecuteReader requires an open and available
          Connection. The connection's current state is closed.




          Cor: What do you mean by detaching the file? Please explain.
          How do I give special access permisions?
          Is it not only required that I add the SQL database item
          into my project?

          Thank you all in advance


          Cor Ligthert [MVP] je napisao/la:
          Axel,
          >
          Can you try it with that to detach file on your local system first. It can
          be that you have to give special access permissions on the Server. This
          seems to me as a known subject.

          Comment

          • axel22@gmail.com

            #6
            Re: Connection to SQL database on local computer

            conn.Open();

            I know, I'm an idiot.

            Thank you for your time, it works now.

            Comment

            • Otis Mukinfus

              #7
              Re: Connection to SQL database on local computer

              On 4 Nov 2006 04:48:05 -0800, axel22@gmail.co m wrote:
              >conn.Open();
              >
              >I know, I'm an idiot.
              >
              >Thank you for your time, it works now.
              There are times when one is able to diagnose someone else's mistakes because
              they have made the same mistakes themselves.

              ;o)
              ;o)


              Good luck with your project,

              Otis Mukinfus


              Comment

              Working...