I have written some codes in vb.net to backup a database in local computer. In many computers it work without problem. but in a specific computer I encounter with this error :*"Timeout expired. The timeout elapsed prior to completion of the operation or the server is not responding The backup or restore was aborted."
With SQL server Management I can backup without problem but with my codes I have problem.
Private Sub BackupDataBase()
Dim cnn_str As String = "Data Source=mycomputer\rb;Database=Master;integrated security=SSPI;"
Dim _backupFileName As String = "d:\backup.bak"
Dim query As String = "BACKUP DATABASE rb_db TO DISK='" & _backupFileName & "' WITH INIT"
Dim cnn As New SqlConnection(cnn_str)
Dim cmd As New SqlCommand(query, cnn)
Try
If cnn.State = ConnectionState.Closed Then cnn.Open()
cmd.ExecuteNonQuery()
MsgBox("Backup successful!")
Catch ex As Exception
MessageBox.Show("Backup failed!" & vbNewLine & Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
A SqlCommand has a CommandTimeout value of 30 seconds by default. If the operation specified does not complete in that time period, an exception is thrown.
If your operation requires more than 30 seconds to complete, set the CommandTimeout to a greater value. The time to execute will depend on the system hardware and current load.
I don't know for sure but I suspect that the backup will still be performed, even if that exception is thrown by your application.
I have a problem. I want to ask you all about how to back up database from vb.net. I'm using PostgreSQL. Why I need back up database? Because it's too large to display all at datagridview when it's running. So for a time span, example 3 months, it's automatically backed-up. I always get Messagebox "Error at or near Backup. Error while executingnonquer
Thank you
koneksi()
Try
Dim fname As String
Dim db2 As String = "aplikasilis2"
Dim strQuery As String
Dim objdlg As New SaveFileDialog
objdlg.FileName = "C : \Documents\Arsip 3 bulan_" + FormatTglUniversal(Date.Now) + ".bak"
objdlg.ShowDialog()
fname = objdlg.FileName
Dim data_affector As Integer
strQuery = "Backup database =" & db2 & " To disk ='" & fname & "'"
Try
cmdBackup = New OdbcCommand(strQuery, conn)
data_affector = cmdBackup.ExecuteNonQuery
MsgBox("berhasil")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
You can't backup Postgresql use query as stated in comment by #jmcilhinney and the link to refer your question it's clear.
In Postgresql Documentation there are no information about that too:
Backup Use pg_dump or Backup
But using pg_dump will give you a full sql file including all DDL and DML statements to recreate your database in another place (or restore).
And Big system too like Odoo or iDempiere as ERP use database Postgresql do backup too using pg_dump command.
Odoo : How To Backup Odoo
iDempiere : How To Backup iDempiere
Just accept it you can't do that from Query statement and make meeting your project about this..
I have SQL SERVER database file in Project folder. The same file copy in Debug folder. I attach these two files in SQL Server. File in project folder table contains Null value in all field. But there is data in the file attached from the Debug folder. I created the connection string with the file in Project folder. Actually which database file is the correct file? Try to solve this problem.
The Connection String is
Public Conn As SqlConnection
Public Function getConnect() As SqlConnection
Conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EMP_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Return Conn
End Function
And this is my code..
Try
getConnect()
Dim query As SqlCommand
Dim strSQL As String
strSQL = "INSERT INTO EMPLOYEE (EMP_ID,EMP_NAME,EMP_FNAME,EMP_GENDER,EMP_DOB,EMP_CAST,EMP_DEPART,EMP_DESIG,EMP_DOJ,EMP_SALARY,EMP_PF_ESI,EMP_BRANCH,EMP_CONTACT,EMP_ADDRESS)VALUES(#EMP_ID,#EMP_NAME,#EMP_FNAME,#EMP_GENDER,#EMP_DOB,#EMP_CAST,#EMP_DEPART,#EMP_DESIG,#EMP_DOJ,#EMP_SALARY,#EMP_PF_ESI,#EMP_BRANCH,#EMP_CONTACT,#EMP_ADDRESS)"
query = New SqlCommand(strSQL, Conn)
query.Parameters.Add(New SqlParameter("#EMP_ID", TXTEMPID.Text))
query.Parameters.Add(New SqlParameter("#EMP_NAME", TXTNAME.Text))
query.Parameters.Add(New SqlParameter("#EMP_FNAME", TXTFNAME.Text))
query.Parameters.Add(New SqlParameter("#EMP_GENDER", gend))
query.Parameters.Add(New SqlParameter("#EMP_DOB", DTPEMPDOB.Value.Date))
query.Parameters.Add(New SqlParameter("#EMP_CAST", TXTCASTE.Text))
query.Parameters.Add(New SqlParameter("#EMP_DEPART", CMBDEPT.Text))
query.Parameters.Add(New SqlParameter("#EMP_DESIG", CMBDESIG.Text))
query.Parameters.Add(New SqlParameter("#EMP_DOJ", DTPEMPDOJ.Value.Date))
query.Parameters.Add(New SqlParameter("#EMP_SALARY", MTXTSAL.Text))
query.Parameters.Add(New SqlParameter("#EMP_PF_ESI", MTXTPFESI.Text))
query.Parameters.Add(New SqlParameter("#EMP_BRANCH", TXTBRANCH.Text))
query.Parameters.Add(New SqlParameter("#EMP_CONTACT", MTXTCONTACT.Text))
query.Parameters.Add(New SqlParameter("#EMP_ADDRESS", RTXTADDRESS.Text))
Conn.Open()
Dim numAffected = query.ExecuteNonQuery()
'MessageBox.Show(numAffected)
Conn.Close()
If numAffected > 0 Then
Call getConnect()
MessageBox.Show("Successfully Added", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information)
BTNCLEAR.PerformClick()
Else
MsgBox("No record was inserted")
End If
Catch ex As Exception
MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Add")
End Try
End If
I change my connection string like this...
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EMP_DB.mdf;Initial Catalog=EMP_DB;Integrated Security=True;Connect Timeout=30;User Instance=False
User instances are depreciated, and probably what is causing this confusion.
To quote SQL Server MVP Aaron Bertrand:
Using User Instance means that SQL Server is creating a special copy
of that database file for use by your program. If you have two
different programs using that same connection string, they get two
entirely different copies of the database. This leads to a lot of
confusion, as people will test updating data with their program, then
connect to a different copy of their database in Management Studio,
and complain that their update isn't working. This sends them through
a flawed series of wild goose chase steps trying to troubleshoot the
wrong problem.
[Source]
He also goes on to list some alternatives in the same post:
Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
If you're using SQL Server 2012, use SqlLocalDb for local development. See: "Getting Started with SQL Server 2012 Express LocalDB."
Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same - so it's not necessarily going to provide you with all the functionality you're ultimately going to want to deploy.
I am using following code to attach my database to SQL Server. The problem is if I create a new file and attach it through my code but when I detach the file using SSMS and again run the same code, it gives error.
Error is:
Unable to open the physical file "e:\dbForATTWithPWD.mdf". Operating
system error 2: "2(error not found)"
The code is:
Dim conn As New SqlConnection("Server=MyHomeServer\SQLExpress;Database=master;Integrated Security=SSPI")
Dim cmd As New SqlCommand("", conn)
cmd.CommandText = "CREATE DATABASE MyHomeWithPWD ON ( FILENAME = 'e:\dbForATTWithPWD.mdf' ), ( FILENAME = 'e:\dbForATTWithPWD_log.ldf' ) FOR ATTACH"
conn.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Dispose()
Why is it happening. All the permissions are the same even first time.
Also what should be the connection string if I need to use DB with uid sa and pwd abc123 ?
Thanks
Try to making sure that nothing else is accessing that mdb file after you detach it. I would probably shutdown that SQLServer instance and restart. Also, I'd make sure that you could detach and reattach in SSMS.
For your connection string try this: "Server=YOURSVR;Database=DB;Trusted_Connection=False;User ID=sa;Password=abc123;"
I created a DB using following code.
Dim conn As New SqlConnection("Server=.\SQLExpress;Data Source=;Integrated Security=SSPI")
Dim cmd As New SqlCommand("", conn)
cmd.CommandText = "CREATE DATABASE MyDBTest22 ON ( FILENAME = 'D:\dbTestATTTTTTT.mdf' ), ( FILENAME = 'D:\dbTestATTTTTTT_log.ldf' ) FOR ATTACH"
conn.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Dispose()
It ran without any error but when I opened SSMS, I could not see my file attached to the server. Also, I tried to make a connection, but it says file does not exist but when I tried to re-run the above code, it says File already exists.
Something wrong with my way of doing it? I want to see it attached with the instance of my SQL Server Express 2005, using SSMS.
Thanks
You're missing a database to connect to in your connection string - if you want to attach a file, I would recommend connecting to the master database:
Dim conn As New SqlConnection("Server=.\SQLExpress;Database=master;Integrated Security=SSPI")