6 Replies - 7979 Views - Last Post: 12 June 2009 - 08:25 PM Rate Topic: -----

#1 LEMarshall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 09

Unable to open physical file SQL 2008

Post icon  Posted 30 May 2009 - 10:00 AM

Using the following code:
Public Class Form1
   
	Private ConnString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Ecologics\Data\EcoLogics.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
	Private searchParameter As String

	Public Sub SelectMyData(ByVal myConnString As String, ByVal searchValue As String)
		Dim daMyName As New SqlDataAdapter
		Dim dsMyName As New DataSet
		Dim mySelectQuery As String = "Select ecosystemid, lastformnum, lastbackup, nextthing, otherthing, lastthing From ecosystem  );"
		Dim myConnection As New SqlConnection(myConnString)
		Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
		myConnection.Open()
		daMyName.SelectCommand = myCommand
		daMyName.Fill(dsMyName)
		If Not dsMyName.Tables(0).Rows.Count > 0 Then
			MessageBox.Show("There were no results found.", "No Results Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		Else
			Field1TextBox.Text = dsMyName.Tables(0).Rows(0)("field1").ToString()
			Field2TextBox.Text = dsMyName.Tables(0).Rows(0)("field2").ToString()
			Field3TextBox.Text = dsMyName.Tables(0).Rows(0)("field3").ToString()
			Field4TextBox.Text = dsMyName.Tables(0).Rows(0)("field4").ToString()
			Field5TextBox.Text = dsMyName.Tables(0).Rows(0)("field5").ToString()
			Field6TextBox.Text = dsMyName.Tables(0).Rows(0)("field6").ToString()
			myConnection.Close()
		End If
	End Sub

	Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
		searchParameter = "" 'searchTextBox.Text
		SelectMyData(ConnString, searchParameter)
	End Sub




I get this error message on the myConnection.Open

Unable to open the physical file "C:\Ecologics\Data\EcoLogics.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". An attempt to attach an auto-named database for file C:\Ecologics\Data\EcoLogics.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Using SQL Server 2008 and VS Team system 2008

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to open physical file SQL 2008

#2 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Unable to open physical file SQL 2008

Posted 30 May 2009 - 10:27 AM

Your connection string is used for accessing a Sql Server database that is disconnected from Sql Server. Since you are running Sql Server, it has a hold of the database, hence the error. Just closing Sql Server will not stop this because it is running as a background process.

Since you are accessing the database via the Instance name, then use the following format for your connection string. Where Initial Catalog is the name of the database.

Data Source=.\SQLEXPRESS;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Was This Post Helpful? 0
  • +
  • -

#3 LEMarshall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 09

Re: Unable to open physical file SQL 2008

Posted 30 May 2009 - 12:37 PM

View PostJayman, on 30 May, 2009 - 09:27 AM, said:

Your connection string is used for accessing a Sql Server database that is disconnected from Sql Server. Since you are running Sql Server, it has a hold of the database, hence the error. Just closing Sql Server will not stop this because it is running as a background process.

Since you are accessing the database via the Instance name, then use the following format for your connection string. Where Initial Catalog is the name of the database.

Data Source=.\SQLEXPRESS;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;



thanks it gets me further but still not all the way there. I used both variations as shown below:
	Private ConnString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=C:\Ecologics\Data\EcoLogics.mdf;User Id=Eupraxia\Larry;Password=;"

	Private ConnString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=C:\Ecologics\Data\EcoLogics.mdf;User Id=Larry;Password=;"




And I get error message Login failed for user 'Eupraxia\Larry'. The user is not associated with a trusted SQL Server connection. or Login failed for user 'Larry'. The user is not associated with a trusted SQL Server connection.
Attached is the login screen from SQL Server so I'm not sure where the difference is.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 mikeblas   User is offline

  • D.I.C Regular
  • member icon

Reputation: 44
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: Unable to open physical file SQL 2008

Posted 31 May 2009 - 08:00 AM

Both of your connection strings specify SQL Server security; your login screen is logging in with Windows integrated security. Windows Integrated security will work for your program if you're the user running it; it's running under your security credentials, and will present those credentials to the database when it tries to log in.
Was This Post Helpful? 0
  • +
  • -

#5 LEMarshall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 09

Re: Unable to open physical file SQL 2008

Posted 31 May 2009 - 04:02 PM

View Postmikeblas, on 31 May, 2009 - 07:00 AM, said:

Both of your connection strings specify SQL Server security; your login screen is logging in with Windows integrated security. Windows Integrated security will work for your program if you're the user running it; it's running under your security credentials, and will present those credentials to the database when it tries to log in.



thanks that got me further along- deep down in MSDN I found an article that said to create a windows Integrated Security do it this way:
Private ConnString As String = "integrated security=SSPI;data source=ECOLOGICSSQL;persist security info=False;initial catalog=EcoLogics;"



But that was replete with errors. After several times I goth this to work:
Private ConnString As String = "integrated security=SSPI;data source=.\ECOLOGICSSQL;persist security info=False;initial catalog=EcoLogics;"



Not sure why the .\ makes the difference but it does. Left one of those pop-up comment boxes on msdn that aappears to be a type error.

However now it get past loging in and opening the connection but now I get this error on the line daMyName.Fill(dsMyName)

Incorrect syntax near ')'.

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings
Imports ADODB
Imports ADOR


Public Class Form1
   
	'Private ConnString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Ecologics\Data\EcoLogics.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
	'	Private ConnString As String = "Data Source=.\ECOLOGICSSQL;Initial Catalog=EcoLogics;User Id=JoeW;Password=JoeW09;"
	Private ConnString As String = "integrated security=SSPI;data source=.\ECOLOGICSSQL;persist security info=False;initial catalog=EcoLogics;"


	Private searchParameter As String

	Public Sub SelectMyData(ByVal myConnString As String, ByVal searchValue As String)
		Dim daMyName As New SqlDataAdapter
		Dim dsMyName As New DataSet
		Dim mySelectQuery As String = "Select ecosystemid, lastformnum, lastbackup, CompanyName From ecosystem  );"
		Dim myConnection As New SqlConnection(myConnString)
		Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
		myConnection.Open()
		daMyName.SelectCommand = myCommand
		[color=#FF0000]daMyName.Fill(dsMyName)[/color]
		If Not dsMyName.Tables(0).Rows.Count > 0 Then
			MessageBox.Show("There were no results found.", "No Results Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		Else
			Field1TextBox.Text = dsMyName.Tables(0).Rows(0)("ecosystemid").ToString()
			Field2Textbox.Text = dsMyName.Tables(0).Rows(0)("lastformnum").ToString()
			Field3TextBox.Text = dsMyName.Tables(0).Rows(0)("lastbackup").ToString()
			Field4TextBox.Text = dsMyName.Tables(0).Rows(0)("CompanyName").ToString()
			myConnection.Close()
		End If
	End Sub

	Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
		searchParameter = "" 'searchTextBox.Text
		SelectMyData(ConnString, searchParameter)
	End Sub

End Class



So I am still at a loss as to how to make a simple connection from a program to the database.

Any other help is appreciated.
Larry
Was This Post Helpful? 0
  • +
  • -

#6 LEMarshall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 09

Re: Unable to open physical file SQL 2008

Posted 31 May 2009 - 04:20 PM

found the stray ")" in the SQL Select statement. It now works. Thanks a lot for all the assistance. Now, I've got to test the code for insert and update that I found in the code snippets but, I'm, sure that will work out fine now.

Any problems, I'll upload for assistance. Thanks again.

Larry
Was This Post Helpful? 0
  • +
  • -

#7 mikeblas   User is offline

  • D.I.C Regular
  • member icon

Reputation: 44
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: Unable to open physical file SQL 2008

Posted 12 June 2009 - 08:25 PM

Happy to help. Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1