Connection to the DataBase

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I have an object SqlConnection and I changed the connection string property
in the properties windows. The object works fine but if I do the same
declaring the object connection by code and with the same connection string
property, doesn't work. It can't connect to the database.

What's happening??

Thank you
 
I think you will need to post your connection string. I do remember having
the same kind of problem once -however one thing to remember is that when
you programatically do it you can have a very basic connection string. That
said the following connection string was taken from the designer property
for connection string and I used it programmatically ok..
this.sqlConnection1.ConnectionString = "workstation id=HOST;packet
size=4096;integrated security=SSPI;data source=HOST;persist security
info=False;initial catalog=Northwind";

this.sqlConnection1.Open();

System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT TOP 10 * FROM
Orders",sqlConnection1);

System.Data.SqlClient.SqlDataReader sr = cmd.ExecuteReader();

if (sr.Read())

System.Windows.Forms.MessageBox.Show(sr[0].ToString());



You will see the following is a very basic connection string (all you
need -bearing in mind you are using Integrated security.

cnn.ConnectionString =

"Data Source =(local);" +

"Initial Catalog=Northwind;" +

"Integrated Security=SSPI";


--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
Hi Alberto,

To make sure you use the right connection string code, you can refer to here:

http://www.connectionstrings.com/

It had many examples on different database and type of connections.

Hope it helps.
--
Regards,
Chua Wen Ching :)


Mark Broadbent said:
I think you will need to post your connection string. I do remember having
the same kind of problem once -however one thing to remember is that when
you programatically do it you can have a very basic connection string. That
said the following connection string was taken from the designer property
for connection string and I used it programmatically ok..
this.sqlConnection1.ConnectionString = "workstation id=HOST;packet
size=4096;integrated security=SSPI;data source=HOST;persist security
info=False;initial catalog=Northwind";

this.sqlConnection1.Open();

System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT TOP 10 * FROM
Orders",sqlConnection1);

System.Data.SqlClient.SqlDataReader sr = cmd.ExecuteReader();

if (sr.Read())

System.Windows.Forms.MessageBox.Show(sr[0].ToString());



You will see the following is a very basic connection string (all you
need -bearing in mind you are using Integrated security.

cnn.ConnectionString =

"Data Source =(local);" +

"Initial Catalog=Northwind;" +

"Integrated Security=SSPI";


--


Br,
Mark Broadbent
mcdba , mcse+i
=============
Alberto said:
I have an object SqlConnection and I changed the connection string property
in the properties windows. The object works fine but if I do the same
declaring the object connection by code and with the same connection string
property, doesn't work. It can't connect to the database.

What's happening??

Thank you
 
Brilliant. However many Favorites I stack up in IE there is always that
extra one that is very useful.
Nice link. Im adding it right now.

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
Chua Wen Ching said:
Hi Alberto,

To make sure you use the right connection string code, you can refer to here:

http://www.connectionstrings.com/

It had many examples on different database and type of connections.

Hope it helps.
--
Regards,
Chua Wen Ching :)


Mark Broadbent said:
I think you will need to post your connection string. I do remember having
the same kind of problem once -however one thing to remember is that when
you programatically do it you can have a very basic connection string. That
said the following connection string was taken from the designer property
for connection string and I used it programmatically ok..
this.sqlConnection1.ConnectionString = "workstation id=HOST;packet
size=4096;integrated security=SSPI;data source=HOST;persist security
info=False;initial catalog=Northwind";

this.sqlConnection1.Open();

System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT TOP 10 * FROM
Orders",sqlConnection1);

System.Data.SqlClient.SqlDataReader sr = cmd.ExecuteReader();

if (sr.Read())

System.Windows.Forms.MessageBox.Show(sr[0].ToString());



You will see the following is a very basic connection string (all you
need -bearing in mind you are using Integrated security.

cnn.ConnectionString =

"Data Source =(local);" +

"Initial Catalog=Northwind;" +

"Integrated Security=SSPI";


--


Br,
Mark Broadbent
mcdba , mcse+i
=============
Alberto said:
I have an object SqlConnection and I changed the connection string property
in the properties windows. The object works fine but if I do the same
declaring the object connection by code and with the same connection string
property, doesn't work. It can't connect to the database.

What's happening??

Thank you
 
Back
Top