Re: Connection to SQL database on local computer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 4 Nov 2006 03:44:14 -0800, axel22@xxxxxxxxx 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.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connectionString =
@"Data
Source=.\SQLEXPRESS;AttachDbFilename=F:\c#\Projects\ConsoleApplication1"
+
@"\ConsoleApplication1\Database.mdf;Integrated
Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand com = new SqlCommand("SELECT * FROM tabela1",
conn);
SqlDataReader reader = null;
try
{
reader = com.ExecuteReader();
}
catch (InvalidOperationException)
{
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
http://www.arltex.com
http://www.tomchilders.com
.



Relevant Pages

  • Re: Serious problems keeping an hold on database support in ASP.NET 2
    ... > The function returns the reader object as return value. ... If the call to dispose throws an exception, ... >>> if not connection is nothing then ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Connection state open even without a server or database?
    ... thus "checking for a dial tone" would throw an exception. ... allowing the user to point the connection wherever they like). ... instantiating and passing the connection string are like ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection state open even without a server or database?
    ... But, as you found out, you'll always get a dial tone. ... allowing the user to point the connection wherever they like). ... That would just mean you get an exception a little earlier. ... instantiating and passing the connection string are like ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Do I need to close DataReader explicitly?
    ... Code readibilty aside, from the operational point of view, if an exception ... occurs in the code that I posted, the connection will be closed and along ... I do not need to explicitly close the reader, ... connection when the DataReader is closed. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Do I need to close DataReader explicitly?
    ... The last who would arguing in that recommendation from you is probably me. ... You're right about the connection and 'using' statement, ... you will need to close your reader) just so that your intention is clear. ... If an exception occurs after acquiring a reader but before closing the ...
    (microsoft.public.dotnet.framework.adonet)