Creating connection string (i think)

K

K Viltersten

I'm getting "connection not initialized" for
error when running the following code.

string conStr =
"Persist Security Info=True;"
+ "User ID=sa;Password=passpass;"
+ "Initial Catalog=TheNameOfDataBase;"
+ "Data Source=LP028\\SQLEXPRESS;";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd =
new SqlCommand("select * from the_table");
con.Open();
cmd.ExecuteNonQuery();
con.Close();

Why?! As far i can see, it should work. The
only part i'm unsure about is the connection
string but it's copied directly from an other
project where it works...

Could it be something simple in the config
file, perhaps? The one i have access to is so
large that it's not possible to get throug it
as a whole... :(
 
J

Jon Skeet [C# MVP]

I'm getting "connection not initialized" for
error when running the following code.

string conStr =
    "Persist Security Info=True;"
    + "User ID=sa;Password=passpass;"
    + "Initial Catalog=TheNameOfDataBase;"
    + "Data Source=LP028\\SQLEXPRESS;";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd =
    new SqlCommand("select * from the_table");
con.Open();
cmd.ExecuteNonQuery();
con.Close();

Why?! As far i can see, it should work.

You've created a connection, and created a command, but you haven't
associated the two.

IIRC there's a SqlCommand constructor overload which accepts a
connection parameter.

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

I'm getting "connection not initialized" for
error when running the following code.

You are missing a single line :)

cmn.Connection = con;
 
I

Ignacio Machin ( .NET/ C# MVP )

You are missing a single line :)

cmn.Connection = con;


BTW, you should code your code in this way:

using (SqlConnection con = new SqlConnection(conStr) )
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select * from
the_table", con) )
{
cmd.ExecuteNonQuery()
}
}

in this way you make sure that the objects are disposed correctly,
even in the presence of an exception.
 
K

K Viltersten

BTW, you should code your code in this way:
using (SqlConnection con = new SqlConnection(conStr) )
{
con.Open();
using (SqlCommand cmd = new SqlCommand
("select * from the_table", con) )
{
cmd.ExecuteNonQuery()
}
}
in this way you make sure that the objects are
disposed correctly, even in the presence of an exception.

This is the first time i see somebody using "using"
that way... It wouldn't be, by any chance, that
crazy v3.0 sytax that no pure sould should ever
use*, would it?

* That is, until i get my boss to caugh up a few
shiny coins and get the VS08 to the office. :)
 
J

Jon Skeet [C# MVP]

K Viltersten said:
This is the first time i see somebody using "using"
that way... It wouldn't be, by any chance, that
crazy v3.0 sytax that no pure sould should ever
use*, would it?

No. The using statement has been in C# since 1.0, and is pretty much
vital unless you want to handcode try/finally everywhere. What are you
currently doing with streams etc, to make sure they're always closed?
 
K

K Viltersten

BTW, you should code your code in this way:
No. The using statement has been in C# since 1.0, and is pretty much
vital unless you want to handcode try/finally everywhere. What are you
currently doing with streams etc, to make sure they're always closed?

I generally use the .close() method on the stream
and i try to do so the very second i issue an
..open() method. Not 100% failproof but that's the
way i do it. Glad to hear there's a nicer method!
I'll make sure to take a look at it. I guess it's
my Java-base that's at blame.

Thanks!
 
J

Jon Skeet [C# MVP]

K Viltersten said:
I generally use the .close() method on the stream
and i try to do so the very second i issue an
.open() method. Not 100% failproof but that's the
way i do it. Glad to hear there's a nicer method!
I'll make sure to take a look at it. I guess it's
my Java-base that's at blame.

In Java you'd still want to use a try/finally block.

The using statement is "just" syntactic sugar for a try/finally block
such that Dispose is always called. It's so handy that they're looking
at getting it into Java 7 (although it's a matter under a fair amount
of debate).
 

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