views:

67

answers:

4

I want to connect to a database on a host except localhost, my DBMS is SQL Server and I'm using ado.net, like this:

SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("insert into st (ID,Name) values ('"+cnt.ToString()+"','havijuri');", con);
//some sql commands.
con.Close();

what should I use as the constr (connection string), and with these information:

  • host IP: 10.15.10.12
  • the file is database1.mdf,
  • in this directory(on the host): D:\Project1\DataBase

Tell me if any other information is needed

+1  A: 

http://www.connectionstrings.com/sql-server-2008

Zane Edward Dockery
Why was this upvoted? Have you read the question?
Marko
It explains all connection string possibilities. Reading it is enlighting.
TomTom
+3  A: 

If the mdf file is not attached to an instance of sql server and you want to connect to the database while it does not exist on the same machine as your application, you need first to copy the database to the server with the mentioned IP and attach it to an instance of sql server installed on that server. The connection string in this case -if you have a domain and will be authenticated to the database server by windows authentication- will be as follows:

"data source=10.15.10.12; integrated security=SSPI;initial catalog=database1"

Or you can create a sql server user on the database sever and connect using the following connection string:

"data source=10.15.10.12; initial catalog=database1;user id=<username>;password=<password>"
SubPortal
A: 

and with these information:

•host IP: 10.15.10.12 •the file is database1.mdf, •in this directory(on the host): D:\Project1\DataBase

You can not. Database file attachment is only supported by express, not by the real server. ForSL Server, you need the database name (which can be different than the file name) and the database must e mounted first by the DBA. You also need acces to the server (as in: username, password). The security credentials are - again - determined by the DBA.

So, you miss the critical information (name of the database, username, password) to access a database server.

TomTom
A: 

create a file on your desktop called test.udl open it up and follow the steps to connect to your database then click test to make sure it works. then open the file in notepad, it will be 1 line and contain the connection string

aaron