Answered by:
How to restore SQL Server database

Question
-
hi
I used this code to restore the database from a specified path or root path, but the following error occurs
my code
Public conA As New SqlConnection("Data Source=DESKTOP-TRRGQMF\SQLEXPRESS2014; Initial Catalog=namedb; Integrated Security=true")
Try cmd = New SqlCommand("restore database namedb from disk='C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS2014\MSSQL\Backup\namedb.bak'", conA) conA.Open() cmd.ExecuteNonQuery() conA.Close() Catch ex As Exception MsgBox(ex.Message) End Try
another way
'Try ' Dim database As String = conA.Database.ToString() ' If conA.State <> ConnectionState.Open Then ' conA.Open() ' End If ' Dim sqlStmt2 As String = String.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE") ' Dim bu2 As SqlCommand = New SqlCommand(sqlStmt2, conA) ' bu2.ExecuteNonQuery() ' Dim sqlStmt3 As String = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + "E:\namedb.bak" + "' WITH REPLACE" ' Dim bu3 As SqlCommand = New SqlCommand(sqlStmt3, conA) ' bu3.ExecuteNonQuery() ' Dim sqlStmt4 As String = String.Format("ALTER DATABASE [" + database + "] SET MULTI_USER") ' Dim bu4 As New SqlCommand(sqlStmt4, conA) ' bu4.ExecuteNonQuery() ' MessageBox.Show("database restore done successefuly") ' conA.Close() 'Catch ex As Exception ' MsgBox(ex.Message) 'End Try
- Edited by ahmeddc Friday, November 13, 2020 6:07 PM
Friday, November 13, 2020 6:03 PM
Answers
-
Hi ahmeddc,
Thank you for posting here
You need to make sure that your account is valid, if not, either create a login on SQL Server for that account, or then specify another valid SQL Server account in your connection string.
Here's a reference you can refer to.
Hope it could be helpful.
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by ahmeddc Thursday, November 19, 2020 3:47 PM
Monday, November 16, 2020 7:44 AM
All replies
-
You blacking out what you're blacking out really means nothing no one cares they can't use it if you showed it. :) Bottom line is you're use Windows autentication, and the logged in user account used to log into the O/S is being presented to the database. The database knows nothing about the user-id and it is being denied access to the databased
- Edited by DA924x Friday, November 13, 2020 9:51 PM
Friday, November 13, 2020 9:50 PM -
Hello,
For reference, see the following for how to login with a user id and password for a valid account.
https://www.connectionstrings.com/sql-server/
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub page- Proposed as answer by KHURRAM RAHIM Monday, December 7, 2020 8:26 PM
Saturday, November 14, 2020 2:37 AM -
Hi ahmeddc,
Thank you for posting here
You need to make sure that your account is valid, if not, either create a login on SQL Server for that account, or then specify another valid SQL Server account in your connection string.
Here's a reference you can refer to.
Hope it could be helpful.
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by ahmeddc Thursday, November 19, 2020 3:47 PM
Monday, November 16, 2020 7:44 AM -
The link you mentioned helped me, ThanksMonday, November 16, 2020 8:23 AM
-
Hi Ashwanit,
If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, November 18, 2020 9:07 AM -
The link you mentioned helped me. Thanks a lot.
Best regards,
Monday, December 7, 2020 1:23 PM