Showing posts with label connects. Show all posts
Showing posts with label connects. Show all posts

Thursday, March 8, 2012

Cannot copy the mdf file after processing

I have a windows service that connects to a regular sql server 2005 database and basically bulk copies data into a SQL express database. Afterwards, I just want to copy the MDF file into a different directory. I keep getting the "Process cannot access file because it is being used by another process" error. I've tried changing the connection strings but nothing seems to work. I'm closing the connections in the code as well. Here is the code. Any thoughts or help would be appreciated. RefreshDB calls Sync 3 times and if all three calls are successful, it attempts to copy the database file to the specified location. This is where I get the error.

Public Function RefreshDB() As Boolean
Dim success As Boolean = True
Dim tables() As String = {"Job", "Equipment", "PMScheduled"}
For Each tableName As String In tables
If SyncTable(tableName) = False Then
success = False
Exit For
End If
Next
If success Then
'copy the new database to the target directory
Try
File.Copy(My.Settings.DBFilePath, My.Settings.TargetDirectory + "\EMField.mdf")
Catch ex As Exception
My.Application.Log.WriteEntry(ex.Message + "(RefreshDB)", TraceEventType.Critical)
Return False
End Try
End If
Return success
End Function


Private Function SyncTable(ByVal tableName As String) As Boolean
Dim reader As SqlDataReader
Dim sourceViewName As String = String.Format("EMField{0}View", tableName)
Dim connectionString As String = My.Settings.EMFieldConnectionString
'insert the path to the database into the connectionstring
connectionString = connectionString.Replace("[DBFilePath]", My.Settings.DBFilePath)
Try
'clear the target table first
Using targetConnection As New SqlConnection(connectionString)
Using truncateCommand As New SqlCommand(String.Format("TRUNCATE TABLE {0}", tableName), targetConnection)
targetConnection.Open()
truncateCommand.ExecuteNonQuery()
targetConnection.Close()
End Using
End Using
'create a datareader from the source database
Using sourceConnection As New SqlConnection(My.Settings.EMLiteConnectionString)
Using readCommand As New SqlCommand(String.Format("SELECT * FROM {0}", sourceViewName), sourceConnection)
sourceConnection.Open()
reader = readCommand.ExecuteReader
BulkCopy(tableName, reader, connectionString)
reader.Close()
sourceConnection.Close()
End Using
End Using
Return True
Catch ex As SqlException
My.Application.Log.WriteEntry(ex.Message + "(sync)", TraceEventType.Critical)
Return False
End Try
End Function

Public Sub BulkCopy(ByVal tableName As String, ByRef reader As SqlDataReader, ByVal connectionString As String)
Try
'bulk copy from source to target database
Using bulkCopy As New SqlBulkCopy(connectionString)
bulkCopy.DestinationTableName = tableName
bulkCopy.WriteToServer(reader)
bulkCopy.Close()
End Using
Catch ex As SqlException
'throw exception back to calling sub
Throw ex
End Try
End Sub

hi,

the database is probably still in use and an active connection is still referencing it, so even if the database autoclose property is set (for SQLExpress created databases), the physical file(s) is/are still locked and file system operations are not allowed..

first verify no active connection is still alive.. consider that connection pooling will keep them in the pool for about 1 minute after the connection has been disposed and released..

again, even if the autoclose property set is the default for SQLExpress instances, consider first "detaching" the database via sp_detach_db before performing file system operations...

regards

|||

Thanks for your help. I can say with certainty that no other process is connected the sql express database because I am testing it on my own machine. That leaves the connection pooling, but I put pooling=false in the connection string. I have never "detached" a sql express database before. So where exactly (ie what database) do I run this sp_detach_db and are there any consequences in doing so? In other words when the service runs again, will I need to reattach the database before connecting to it?

Thanks,

Bob

|||

hi Bob,

Coach24 wrote:

Thanks for your help. I can say with certainty that no other process is connected the sql express database because I am testing it on my own machine. That leaves the connection pooling, but I put pooling=false in the connection string. I have never "detached" a sql express database before. So where exactly (ie what database) do I run this sp_detach_db

sp_detach_db "unregister" the database from it's related SQL Server instance so that file system operations are allowed on the database's physical files..

and are there any consequences in doing so? In other words when the service runs again, will I need to reattach the database before connecting to it?

Thanks,

Bob

no consequences but, obviously, any connection to the unregistered database will fail until you "reattach" it via the

USE [master]

GO

CREATE DATABASE [test] ON

( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data\test.mdf' ),

( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data\test_log.LDF' )

FOR ATTACH;

GO

statement... so, yes, you have to reattach the database before referencing it..

regards

|||

While I'm certain your suggestion works, I ended up not needing it. As it turns out, even though I had pooling = false in the connection string, and I closed the connection in the code, the connection was still open when I attempted the File.Copy statement. I put a System.Threading.Thread.Sleep(5000) right before the File.Copy statement and that fixed the problem. Thanks again for your help.

Bob

Friday, February 24, 2012

cannot connect to SQL Server from VB.NET

I have an application written in VB.NET that connects directly to a
SQL Server 2000 database over the internet. It works fine on the
development machine (XP Pro), as well as two outside machines running
XP Pro. It does NOT work on two other outside machines, both running
Win2000. All 4 outside machines have the latest .NET framework
installed. I assume that XP has something by default that the 2000
machines do not, but I have no idea what that something could be that
would affect my software. I tried installing MDAC 2.8 as well as the
"sqlredist.exe" on both 2000 machines, but no dice. Is there anything
else I need to install, which XP just happens to already include? The
code doesn't appear to be the issue, since it works on the two XP
machines. And permissions and/or firewalls don't appear to be an
issue for the same reason. Here is the code in question, if that
helps:

Public conTemp As SqlClient.SqlConnection

conTemp = New SqlClient.SqlConnection("Data
Source=xxx.xxx.xxx.xxx\servername;Network Library=DBMSSOCN;Initial
Catalog=somename;User ID=username;Password=password;")

conTemp.Open() 'this is the line where the error occurs

The actual error is "Object reference not set to an instance of an
object", as though the second line above was absent (I get the same
error if I comment out the second line of code and run it on the
development machine). I thought that maybe certain SQL Server drivers
were needed in order for the SQLConnection to operate correctly, but I
assume the MDAC and "sqlredist.exe" would have taken care of that if
it was the problem. I'm out of ideas and appreciate any you guys
might have.

AndrewHi

I can't think of an obvious reason for this. The XP firewall could
have caused problems, but it seems to be the reverse of that,
therefore is there any third party application that is being run
instead on the 2000 machines or do these machines have something else
using the port you are using?

You don't say if the XP and 2000 machines are at the same location, on
the same network/segment?

Are the network configurations the same?

Can you talk to other SQL servers?

HTH

John

magicsoft714@.yahoo.com (Andrew) wrote in message news:<888f9cb6.0311261307.2c59bde7@.posting.google.com>...
> I have an application written in VB.NET that connects directly to a
> SQL Server 2000 database over the internet. It works fine on the
> development machine (XP Pro), as well as two outside machines running
> XP Pro. It does NOT work on two other outside machines, both running
> Win2000. All 4 outside machines have the latest .NET framework
> installed. I assume that XP has something by default that the 2000
> machines do not, but I have no idea what that something could be that
> would affect my software. I tried installing MDAC 2.8 as well as the
> "sqlredist.exe" on both 2000 machines, but no dice. Is there anything
> else I need to install, which XP just happens to already include? The
> code doesn't appear to be the issue, since it works on the two XP
> machines. And permissions and/or firewalls don't appear to be an
> issue for the same reason. Here is the code in question, if that
> helps:
> Public conTemp As SqlClient.SqlConnection
> conTemp = New SqlClient.SqlConnection("Data
> Source=xxx.xxx.xxx.xxx\servername;Network Library=DBMSSOCN;Initial
> Catalog=somename;User ID=username;Password=password;")
> conTemp.Open() 'this is the line where the error occurs
> The actual error is "Object reference not set to an instance of an
> object", as though the second line above was absent (I get the same
> error if I comment out the second line of code and run it on the
> development machine). I thought that maybe certain SQL Server drivers
> were needed in order for the SQLConnection to operate correctly, but I
> assume the MDAC and "sqlredist.exe" would have taken care of that if
> it was the problem. I'm out of ideas and appreciate any you guys
> might have.
> Andrew|||I don't have any other SQL Servers which I can test against, and
installing SQL Enterprise Manager on the 2000 machines to see if that
works and/or fixes the issue isn't really an option I'd like to take.
The XP and 2000 machines are all in the same house, on the same
network. There isn't anything installed on the 2000 machines which
would interfere with my app. As far as I can tell, everything is
equal, other than the OS.

Andrew

> Hi
> I can't think of an obvious reason for this. The XP firewall could
> have caused problems, but it seems to be the reverse of that,
> therefore is there any third party application that is being run
> instead on the 2000 machines or do these machines have something else
> using the port you are using?
> You don't say if the XP and 2000 machines are at the same location, on
> the same network/segment?
> Are the network configurations the same?
> Can you talk to other SQL servers?
> HTH
> John|||I don't have any other SQL Servers which I can test against, and
installing SQL Enterprise Manager on the 2000 machines to see if that
works and/or fixes the issue isn't really an option I'd like to take.
The XP and 2000 machines are all in the same house, on the same
network. There isn't anything installed on the 2000 machines which
would interfere with my app. As far as I can tell, everything is
equal, other than the OS.

Andrew

> Hi
> I can't think of an obvious reason for this. The XP firewall could
> have caused problems, but it seems to be the reverse of that,
> therefore is there any third party application that is being run
> instead on the 2000 machines or do these machines have something else
> using the port you are using?
> You don't say if the XP and 2000 machines are at the same location, on
> the same network/segment?
> Are the network configurations the same?
> Can you talk to other SQL servers?
> HTH
> John|||Hi

The easiest way to check the network would probably be to telnet into the
given port on the server. This will show that the routing is ok. If that
works it is probably something to do with the machine/software
configuration.

John

"Andrew" <magicsoft714@.yahoo.com> wrote in message
news:888f9cb6.0311271609.5a68472b@.posting.google.c om...
> I don't have any other SQL Servers which I can test against, and
> installing SQL Enterprise Manager on the 2000 machines to see if that
> works and/or fixes the issue isn't really an option I'd like to take.
> The XP and 2000 machines are all in the same house, on the same
> network. There isn't anything installed on the 2000 machines which
> would interfere with my app. As far as I can tell, everything is
> equal, other than the OS.
> Andrew
> > Hi
> > I can't think of an obvious reason for this. The XP firewall could
> > have caused problems, but it seems to be the reverse of that,
> > therefore is there any third party application that is being run
> > instead on the 2000 machines or do these machines have something else
> > using the port you are using?
> > You don't say if the XP and 2000 machines are at the same location, on
> > the same network/segment?
> > Are the network configurations the same?
> > Can you talk to other SQL servers?
> > HTH
> > John

Sunday, February 12, 2012

Cannot Connect to an MS SQL Server

Hi,

I've developed an application that connects to an MS SQL server to read and write from a database. I have tested the program on about 6 computers (all on different networks and internet connections, etc...) and they can all access the database remotely just fine.

However, today I tried it on another PC (i.e. the 7th computer) and for some reason it won't connect. This computer is definitely on the internet as I can surf the web with it just fine, but every time I try to run the program it comes up and says that it cannot connect to the database. I can't remember the exact error message, but it was behaving as if the computer wasn't on the internet. What might cause a problem like this?

I did notice that this PC is running Norton Antivirus. All the other computers I tested on were not using Norton. Could Norton be the possible cause (i.e. might it be blocking a port or something like that). I will test it without Norton running, but I just thought I'd ask here too to see if anyone knows other possible reasons why this would be happening.

Thanks.

Have you tried to ping the IP/DNS name of the SQL Server machine?|||moving to SQL Server Data Access forum.|||

Yes. Norton Antivirus could be the cause of the issue. Try run your app with Norton disabled. I assume all your 7 machines are clients and the SQL Server is always the one.

One more thing you can check is to see if you have outdated alias defined on the 7-th machine. Use c:\windows\system32\cliconfg.exe or SQL Server Configuration Manager (if you app use sqlncli.dll) to check it.