Can't gain access to my database created though Visual Studio? - sql-server

I have created a database emailDatabase, its stored in
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
In Visual Studio, I use the Server Explorer, click the add new connection button.
The add connection dialog box appears.
Under server name I use the dropdown box and select DEV-5\SQLEXPRESS. I use Windows authentication.
In the Connect to section at the bottom, the dropdown displays: Master, Model, msdb and tempdb and does not display my emailDatabase.
So I select Attach Database File and click browse and follow
local C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
and select my email database.
The following error occurs :
emailDatabase
You don not have permission to open this file.
Contact file owner or an administrator to obtain permission.
I think my problem is i saved my database wrong, I need to make a back up or something like that. if that's the case please tel me how to make a backup and so on. I really need to move forward from this problem.
When I created my database I right-clicked on databases in SQL Server Management Studio and said new database, then I added columns with a query. then file save all.
How can I get a copy of my database file with all the permissions I need to use it in visual Studio??

When you create a database on the server (using SQL Server Management Studio), you don't have to (and should not!) fiddle around with the database file(s) anymore - let the server handle that for you.
Instead: do a Add Connection in Visual Studio and then specify the server instance (DEV-5\SQLEXPRESS) and the database name (emailDatabase) in your connection dialog.
With this, you're connecting and using a SQL Server database the way it's intended to be used - on the SQL Server instance itself. This is much easier, and much less hassle, than having to struggle with "free-floating" .mdf files and attaching them to your solutions and stuff like that....
So here - fill in DEV-5\SQLEXPRESS into your "Server name" dropdown, and then use the "Select or enter database name" option and enter your database name (or pick it from the dropdown) - it should be there!
DO NOT use the "Attach a database file" option - this is the free-floating .mdf "feature" which is rather clumsy and hard to use and error-prone - again: I recommend not using that...

Had the same problem and I realised the problem was not in VS2010 but my SQLserver.My instance name is OMAFANO ,and that's what my MSSQL connected to under Server Name. Now here's the catch,click on that and connect to OMAFANO\SQLEXPRESS and create all your databases and tables there if you want them to show up in VS2010 the way u stated up there. So under server name in VS2010 also write INSTANCENAME\SQLEXPRESS if you want to see your newly created databases etc. Take a look at the picture:

I thought I had it figured out but problems continue to pop up. So ...
IGNORE EVERYTHING BELOW THIS LINE.
After hours of tinkering I finally figured out how to use SSMS to connect to a SQLServer 2008 database that was created in VS2010, and it is relatively simple. But from the number of unanswered questions all over the place not well documented. Here's how to do it:
In VS right click the project in SolutionExplorer and select Add new item then select Data then SQLServer database. It will prompt you to save it in the app_data folder and create the folder for you if it doesn't exist.
Find the Ssms.exe file (on my system it's in C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE
Right click Ssms.exe and select Run as administrator.
After SSMS opens you can attach the project MDF. It will now be on your list of databases. The database name is a GUID.
You can even run aspnet_regsql against the database to add the Membership tables and sprocs.
IMPORTANT! You can manage this database with EITHER SMSS OR VS but not both at the same time. If you mess with the database from within VS using ServerExplorer you will break the SSMS connection. To fix that you will need to detach and reattach the database.

Run Your Visual studio As Administrator
Go to Visual Studio instance i.e C#,C++ etc
Right click > Run as Administrator
Then now It may work

To connect to a ssms2014 database from visual studio 2013, in the new connection wizard I had to change 'data source' from 'ms sql server database file' to '.net framework data provider for sql server..'. Then I was able to enter [computer name][username] for windows authentication.

I had the same issue, you just type your local server name "sara-PC" instead of
"sara-PC\SQLEXPRESS"
Now you can access your database easily, you can see it in your dropdownlist.
And also please dont use file access method to attatch database, thats not good way.
Also you can put ~....\Data and ~...\log file wherever you want by setting default location using server->rightclick->properties->Database settings.
Definitely this solves your issue.

Related

Can't see backup folder in MS SQL Server 2019 [duplicate]

I am trying to run a classic asp project on visual studio and therefore I am trying to restore a database. I cannot seem to find the .bak file when trying to restore it. It wasn't visible in the C-drive. I have looked it up and have seen that it has something to do with permissions. I have tried enabling permissions after properties>security of .bak file to 'Everyone', it did not work. I have also tried NT Service\MSSQLSERVER. Please see the following below.
I had the same issue. I get to know that it was a a permissions issue.
Here are the following steps to solve this problem
Go to Control panel > Administrative Tools > Services double click on the services.
copy the log on account name of sql service as shown in the picture.
Go to your backup folder and right click and select properties.
Follow the steps as shown in picture.
After clicking on edit you will get new window.
Click on add
Paste here the Log on account name of sql service account which you had copied earlier. then Click on check name button.
Select MSSQLSERVER then click ok.
Now you will get your sql server account name in security tab.
Select sql server account name that and in permission box select Allow in all check boxes(I added Full Control) then click apply.
Now you will be able to select and restore your backup file.
You also have to enable permissions to the folder where the file is located. SSMS first needs to browse the folder, then to read the file.
In your case, SQL server tries to access the folder using NT Service\MSSQLSERVER account, so permissions should be granted to that account, no need to give more permissions (to everyone).
Also try below if above doesn't help:
close/open SSMS
move the file from root to some folder and grant permissions to that folder
use TSQL script to restore the database instead of SSMS
you can copy the backup folder in to C:\Users\MSSQL$HCMIS\Desktop sql server users folder.Now it will be accessible
I'm facing simpler issue for sql server standalone 2014 enterprise edition ,installed in my home system . I tried every possibility provided in various article but strangely the issue is with my 2 named instance .
I can restore database on my default instance with query and GUI . I tried changing log-on as account of sql server service for named instance to 'network service' or 'local system' and giving them full control permission to backup holding folder .
In my (and probably a typical/common case): I looked through (and compare with other .bak files) in the windows permission and see nothing wrong.
However - This may be due to that one does not follow to restore from the [device] option first. Even you store the .bak in the right path, it is initially from the [Source]->[Database] check box, until you follow the steps to restore via the [device] option
(In my case the second AdventureWorksDW2020-DAX-Docs was not available until I go thru extra steps to restore it via the [Device] option first.)
The SQLserver need to do internal access right grants to users... etc..(even all the needed Windows properties are in place) –
Reference https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16&tabs=ssms

Error deploying database to SQL Azure

I'm using forms authentication to handle users and attempting to deploy my database to SQL Azure, but getting this error message:
The only table of the four listed that I utilize is aspnet_Membership, and the only other table I use is aspnet_Users from implementing forms authentication. What is TextInRowSize and why does SQL Azure care about it? Do I have any option to modify aspnet_Membership to make it compatible?
If it would be easier to remove the current system altogether and replace it with my own, I'm fine with that too.
That looks like an error in the data-tier application framework. I can suggest a workaround to get your database to Azure:
Use SqlPackage.exe (https://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx) from the command line to extract a dacpac file with all table data. Then use SqlPackage.exe to deploy that dacpac file to your database in Azure. The extract command would look something like:
C:\>"c:\Program Files\Microsoft SQL Server\120\DAC\bin\SqlPackage.exe" /a:extract /scs:"Data Source=yourSqlServer;Integrated Security=true;Initial Catalog=yourDatabase" /tf:C:\temp\mydatabase.dacpac /p:ExtractAllTableData=true
And the import command would look something like:
C:\>"c:\Program Files\Microsoft SQL Server\120\DAC\bin\SqlPackage.exe" /a:publish /tcs:"Data Source=yourAzureSQLServer.database.windows.net;User Id=yourUserId;Password=yourPassword;Initial Catalog=yourDatabase" /sf:C:\temp\mydatabase.dacpac
So quick suggestions since I see you are using V12 which should support those properties now.
First make sure you are using SSMS 2014 SP1 at least, this has a number of fixes for using V12. Secondly make sure you install the May 2015 update to DacFX (which is the program that creates bacpac files) you can install it here: http://www.microsoft.com/en-us/download/details.aspx?id=46898
This should get you to the best possible chance of your import/export working.
This was solved by generating a SQL Azure script for the DB, and running it on Azure. Here's how I solved it:
First, open SQL Server, right click the database you want to transfer and click "Tasks > Generate Scripts..."
Next, click "Advanced" on the scripting options panel and find the row "Script for the database engine type." Select "Windows Azure SQL Database" and click OK (Note: if you have data that you want to transfer as well, choose "Schema and Data" from the "Types of data to script" option).
Proceed thru the rest of the script generation dialog, remembering where you saved the script file. Connect to your database server using SQL Server or windowsazure.com. Generate a new query for your new database, enter the script that was generated by SQL Server and execute.
Seems like TextInRowSize stores large data for older SQL Server types such as text and ntext. You would need to change it's type to nvarchar(max).
Here's a link to a more detailed explanation.
http://www.dnnsoftware.com/wiki/unsupported-property-textinrowsize-set-and-is-not-supported-when-used-as-part-of-a-data-package

Why doesn't VS show my created database in the 'Select or Enter a Database name' dropdown list?

I created a database named sample in SQL server management Studio 2008 and created a table too. But When I wanted to connect to the created database from the Visual Studio 2010, it did not show my created database.
All I did is I clicked ..
Tools --> Connect to Database --> Selected the server name --> Clicked 'Use Windows Authentication'---> clicked the drop down list of 'Select or enter a database name'.
It dint show my created database sample but showed master, model, msdb, Reportserver, reportserverTempdb, Tempdb.
If I enter the database name, I get the following error:
"This connection cannot be tested because the specified database does not exist or is not visible to the specified user"
What could be the problem and how do I solve it? I am looking forward to solve this problem as soon as possible.
Hope you must have resolved this... otherwise I followed the following steps
Goto SQL Server Object explorer
Add the SQL server - your_pcname\SqlExpress
Add the new database
Create tables into it
Then - try to create the connection; it should show the new database created.
For someone who came here just like me, in my sql server, there was a failed restore attempt of the same database. It said XYZDatabase (Restoring...). The issue got resolved when I deleted that failed restore.
Mention the correct server name of the specific database to which you want to connect.
Open SQL Server > Right click on the database which you want to connect > properties> Click View Connection Properties (In the left side written in blue color)> There is the server name, copy that and paste it in the Connection Server Name in Visual Studio Project on which you want the data.
I hope this helps.

How to generate and connect to database from entity data model in Visual Studio 2010?

I read a few of the corresponding questions here but none of them seems to solve my problem.
I have a .edmx file. I want to generate a database from this model, so I do the following:
Right-click on the entity data model, then Generate Database From Model. Generate Database Wizard appears and asks where to save a generated .sql file. I can only click on Finish.
Now I have an sql file. I run the script - still inside Visual Studio - and it says everything is ok, rows affected etc.
So I suppose I have a database now. (Although I don't know the path of the .mdf file, nor can I look into it to see if the tables have been generated correctly, but that's not the point.)
Visual Studio says I'm connected, though I can't see this database in Server Explorer.
So I run the program, and an exception appears:
EntityExcpetion was unhandled. The underlying provider failed to open.
What's the story with this?
When in your .edmx design window, click anywhere outside of an entity, and view the properties window (F4 is it is not visible). In the properties window, look at the connection string value, and determine if it is valid. You can also see where it is connecting, and thereby confirm the tables are generated correctly.
The connection string is also saved in the App.Config file.
It seems like you are logged in to the database using Visual Studio, but the login does not have all the details correct in the App.Config or there is a permissions issue between the two logins.
When you run your query in Visual Studio, does it ask you to connect to SQL Server? if so, what is the server you are connecting too? I'm thinking you are creating your database in a different server that the one in your project's connection string.

SQL Server Copy Database Issue

I'm running the copy database wizard on a 2008 R2 instance of SQL Server.
The database I want to copy is a SQL 2000 database.
I'm copy that database to another SQL SErver 2008 R2.
The wizard uses SQL authentication for both servers, and both are sysadmins.
When I run it, I get the following error (FYI I have tried both copying the logins and leaving them out):
Event Name: OnError
Message: ERROR : errorCode=-1073548784 description=Executing the query "sys.sp_addrolemember #rolename = N'RandomRoleName..." failed with the following error: "The role 'RandomRoleName' does not exist in the current database.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
helpFile= helpContext=0 idofInterfaceWithError={C81DFC5A-3B22-4DA3-BD3B-10BF861A7F9C}
StackTrace: at Microsoft.SqlServer.Management.Dts.DtsTransferProvider.ExecuteTransfer()
at Microsoft.SqlServer.Management.Smo.Transfer.TransferData()
at Microsoft.SqlServer.Dts.Tasks.TransferObjectsTask.TransferObjectsTask.TransferDatabasesUsingSMOTransfer()
Any help would be appreciated!
Jim
My suggestion is dont use the copy database wizard. Create a full backup of the database on the 2000 server and then restore it on the 2008 server.
If you google "Microsoft.SqlServer.Management.Dts.DtsTransferProvider.ExecuteTransfer Copy Database Wizard" you will find that many many people have gotten this same error or other nearly identical smo errors... no-one appears to have gotten past it.
That's isn't to say its impossible... just, restoring a backup is so much easier then the wizard or troubleshooting the wizard. Good luck.
The copy wizard had missed some security and IIRC it's caused by subtle differences in security tables, principals etc between the 2 versions.
Frankly, the easiest way is to do one of these two:
backup/restore
detach, copy, attach
If you don't have access to the O/S and can't get it, another option is to create the missing role(s) in the background as the copy runs. You have to catch it between the creation of the files and when it tries to reference the roles, but there are a few seconds in which to create them if you keep clicking execute - I managed to create 9 roles.
Unfortunately, you'll end up with the roles in another database too (while yours cannot be used) so those need to be deleted.
Of course, this is only an option when you really can't use the other method.
Though the answer which is using the backup technique solves the problem generally, after facing the same issue several times, I was able to trace down the root of the problem using the Event Viewer of Windows to that the Database Copy wizard, using the SQL Agent, will eventually create a Job for the agent to run, after which the Agent will run using its own credentials (i.e. the credentials that you can look up in Windows Services, in my case, NT Service\SQLAgent$SQL2014)
All you need to do is to go the folder where SQL Server creates DB files (e.g. C:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\DATA by default for SQL 2014) and give the SQL Agent windows user write/read access on the folder.
The reason can be that a file with the new Database name already exist on the filesystem. We encountered this when we renamed Database X to X_Old, and tried to copy database Y to X. This cannot be done, because database X_Old is still associated with the filename X.
Either delete the conflicting database, or rename the file on the file system.
See http://codecopy.wordpress.com/2012/01/03/error-while-copying-a-database/

Resources