Connection to SQL database on local computer

A

axel22

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;
}
 
O

Otis Mukinfus

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
 
C

Cor Ligthert [MVP]

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
 
O

Otis Mukinfus

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;
}
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
http://www.arltex.com
http://www.tomchilders.com
 
A

axel22

Otis: This is the exception I got:

An unhandled exception of type 'System.InvalidOperationException'
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:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top