0

Hello I'm trying to run a simple sql command on a DB from MS VS C# 2010 and I have encountered a error I have never seen before the relevant code is:

  SqlConnection comCon = new SqlConnection(@"Data Source=C:\\Users\\George\\Desktop\\programming\\C#workspace\\Projects\\Examen\\Examen\\Companie.mdf;Initial Catalog=Proiect;Integrated Security=True"); 

  SqlCommand cmd = new SqlCommand();

  cmd.CommandText =  "UPDATE Proiect SET Buget = Buget + 500 WHERE (Buget > 0)";

  cmd.Connection = comCon;                                                      
  comCon.Open();
  Console.WriteLine(cmd.ExecuteNonQuery().ToString());
  comCon.Close();

And the error is Keyword not supported: 'data source'

The main problem is that I'm not used to creating these sqlconnections by hand so please tell me if I'm missing something.

  • 1
    connection strings dont point to physical files , rather Data Source points to DB Engine Instance name.check this link 'http://www.connectionstrings.com/sql-server-2008' – Furqan Hameedi Jun 07 '11 at 07:34
  • Thank this has helped but now it gives me this error Directory lookup for the file "C:\Users\George\Desktop\programming\C#workspace\Projects\Examen\Examen\Companie.mdf" failed with the operating system error 5(Access is denied.). Cannot attach the file 'C:\Users\George\Desktop\programming\C#workspace\Projects\Examen\Examen\Companie.mdf' as database 'Companie'. – Bronze_Hero Jun 07 '11 at 07:42

2 Answers2

1

You are using the wrong structure. To attach a database file, you need to use the following structure:

SqlConnection sqlConnection = 
    "Server=DatabaseServerName;AttachDbFilename=d:\Database\Database.mdf;
     Database=DatabaseName; Trusted_Connection=Yes";

You need to have the right permissions on both the target file and database server to attach the databse and establish the connection.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • @Bronze_Hero: You do not have access permissions for the current windows user on the file you are trying to attach ... – Akram Shahda Jun 07 '11 at 08:00
  • Then how would I obtain them? This is my computer and I just created a simple database – Bronze_Hero Jun 07 '11 at 08:01
  • @Bronze_Hero: Since that is your computer and I guess you are using an administrator account, I guess the problem may be something else. How have you created this database? Have you used sql server management studio ? – Akram Shahda Jun 07 '11 at 08:11
  • No I created it form C# add new Item database – Bronze_Hero Jun 07 '11 at 08:18
  • @Bronze_Hero: Well, try to obtain the connection string by doing the following: from the server explorer panel, right click the connection and select properties. The properties panel will be activated. Use the value in the Connection String field... – Akram Shahda Jun 07 '11 at 08:42
  • Thank you I've already did that,but to no avail, it's become obvious that the error lies in the method I created the db,I will start from scratch – Bronze_Hero Jun 07 '11 at 16:01
0
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

If it's not an ASP.NET application don't use the DataDirectory syntax and just use the full c:... path.

Aravind
  • 4,125
  • 1
  • 28
  • 39