How can i test my TSQL syntax? - sql

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data.
I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck.
I also tried using an .mdf file instead thinking it's a file and I won't have connectivity issues. Wrong, I still couldn't connect and I used this site to help me (i'm aware its 2005)
In that case I used:
var conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;Database=dbo;Trusted_Connection=Yes;");
exception
Unable to open the physical file "C:\dev\src\test\SQL_DB_VS_Test\test.mdf".
Operating system error 5: "5(Access is denied.)".
Cannot attach the file 'C:\dev\src\test\SQL_DB_VS_Test\test.mdf' as database 'dbo'.
With trusted = no I get Login failed for user ''. (What user am I suppose to set?). I created the .mdf with Visual Studio somehow.

What if you try this connection string:
var conn = new SqlConnection(#"Server=.\SQLExpress;
AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;
Database=test;Integrated Security=SSPI;");
I don't think it's a good idea to call your database "dbo" (that's a SQL Server keyword - I wouldn't use it for my own purposes!), and also I believe you need to use Integrated Security=SSPI; to define Windows authentication - Trusted_Connection is not used for SQL Server connection strings, AFAIK.

Have you tried using SSMS to access your local instance? It's helpful for getting connected and getting everything setup. Also, I think the default install of Sql Express with VS only support trusted connections.

After creating the mdf file with visual studios right click the mdf and select properties. In it you'll see a row called Connection String. Copy/paste it into your app and it should connect. The key part is User Instance=True

Related

Problems deploying SSAS project to server

I'm using Visual Studios 2012 and SQL Server 2014. Here's my problem. If I deploy my SSAS solution to 'localhost' everything runs smoothly. When I try to deploy it one of our other servers I receive these errors. I’ve tried changing the accounts in ‘Impersonation Information’, but still get the same errors. I've also made sure I have all admin rights. It’s going on two days and I still cannot find the fix. Errors: enter image description here
"Class not registered" error indicates your data source (SQL) connection string is using a driver not installed. For example if your connection string uses a SQL2008 driver accidentally (with a Provider=SQLNCLI10.1 connection string) it will give this error when you try to process on the server.
Three options to fix it:
Fix the connection string in Visual Studio to use the version of driver that's on the server before you deploy.
Install the version of SQL Server Native Client you use on the server
Deploy without processing the cube, RDP to the server and open SSMS and connect to SSAS and fix the connection string then process.
Thank you for the help.
I had to create the SSAS project in BIDS 2008. Also, I had to delete the dimension created when the cube was created. I then created a new dimension using multiple columns as my unique key. Then, attached new dimension to the cube.

SQL Server authentication for a Database file

I use Microsoft Visual Studio Professional 2013. I created Database1.mdf in my App_Data folder. By default, it uses integrated Windows authentication. I have no problem connect to this database file, but when I update all file into web space and browse it in browser, it has an error. I think I need to change it to SQL Server authentication instead of integrated Windows authentication.
My questions are:
How can I change it to SQL Server authentication step by step? I want to connect this data file with user name & password.
How to assign user into it?
You need to attach the database file to your SQL Server:
http://www.youtube.com/watch?v=rhIr9Qf-oHw
Then you create the type of logins you want:
http://www.youtube.com/watch?v=Uh5USR7pymE
You can now detach the database. Your application can use now the new logins. If you change from Windows to User/password your connection will need to say so, something like:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
I also found this helpful link that describes also the steps:
SQLExpress - How do I set username/password for a sql datafile
Hope this helps.

"This database file is not compatible with the current instance of SQL Server"

I am currently reading through a book about MVVM and I need to use the Northwind database for the next section. Unfortunately, when I try to add Northwind to my project, I get the following error:
"An error occurred while connecting to the database. The database might be unavailable. An exception of type 'Microsoft.Data.ConnectionUI.SqlDatabaseFileUpgradeRequiredException' occurred. The error message is: 'This database file is not compatible with the current instance of SQL Server.'"
I haven't installed an SQL Server, just Visual Studio 2012. The book instructs me to add the .MDF file to the project, set it as an Entity Data Model, and then to generate from database. It's on the next screen where I get the error. I use NORTHWND.MDF as the data connection and the entity connection string is as follows:
metadata=res:///Model1.csdl|res:///Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\NORTHWND.MDF;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
In the book, this reads:
metadata=res:///Model1.csdl|res:///Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;UserInstance=True"
I have tried using both data sources and neither work.
Also of note, the sample code that goes along with the book doesn't work either. It compiles and launches, but in the server explorer, NORTHWND.MDF has an 'x' in the icon and upon launch none of the data is there.
When I attempt to upgrade the database, I get an error saying:
"Database '...\NORTHWND.MDF' cannot be upgraded because its non-release version (539) is not supported by this version of SQL Server. You cannot open a database that is incompatible with this version of sqlservr.exe. You must re-create the database..."
If anybody could give me any tips on how to get this working, I would certainly appreciate it.
my simple way:
go to VisualStadio 2012
In Server Explorer Windows
open Data Connections
Right click on ASPNETDB.MDF
click on Modify Connection...
a new windows open for you ... you just click OK
if other windows open for you click Yes
FINISH ( be Happy ) :D
If we are trying to attach .mdf file in SQL Express mode in our development project and while adding .mdf file in App_Data folder we are getting exception like below :
"The database file is not compatible with the current instance of sql server...."
Then we can go to Server explorer window → go to Data connections → go to "your database" → right click on Modify connection → check the Data sources and Database file name, then use windows Authentication and press OK.
It works for me.
one way to potentially fix it is, install Sql Server 2012 Express and use database connection to sql server. Do not use local database. That might work. Just a suggestion.
Also as an extra note, you might want to download SQL Server 2012 Management Studio (I believe it comes with the above download as a component that you have select). With Management Studio Installed, you can test the database connectivity separately, create tables, run queries etc. This will make troubleshooting a lot easier.
Visual Studio doesn't install any database server by default so you can't connect to one. The MDF file is just the file where SQL Server stores its data, it's not a standalone database.
You have to install some version of SQL Server 2012 Express for both connection strings to work.
The first connection string uses the LocalDB feature which is essentially a minimal installation of SQL Server 2012 Exrpess (~ 35 ΜΒ). You can only connect to LocalDB from your machine which makes it useful only for development purposes.
The second connection string connects to an actual instance of SQL Server Express, called SQLExpress, installed on your local machine. The second connection string attaches the MDF file to the server as a User Instance, which means that the file is released when you stop using it.
The SQL Server Express downloads range from 130 MB to 1.3 GB depending on which version you download (minimal to full with advanced services).
Northwind database is not readily compatible with 2012, the compatible version of Northwind database for sql server 2012 can be downloaded from the below link, and after downloading .mdf file we can easily attach it with sql server 2012. I have found the following and it helped me, hope it will help you. How to Attach Northwind Database in Sql Server 2012
First go to data connection as on pic:1
2.Then browser your mdf file press ok and it will automatically convert the database.
picture:01
picture:02
Modify connection may solves the problem, but I am not sure whether the type of this database file is changed.
I have found a way to solve this problem if you have installed the database Server corresponding to the database file. You just need to change the default database Server of your visual studio.
On the menu, choose Tools-> Options -> Database tools, then on the data connections enter the SQL server Instance name corresponding to your database file which you have installed,then press Ok.
Next step is also important, it is necessary to click 'refresh' or delete the database filename in Data Connection and reopen the file, the file will be added to Data Connection again and open successfully.
This is what worked for me:
On the Add Connection dialog box, click Advanced.
Scroll down to the bottom and look for a field called Data Source.
Change it from (localdb)\MSSQLlocalDB to .\SQLEXPRESS.

SQL Server Express unable to attach or delete mdf file

My database file Whist.mdf was attached to a SQL Server Express through Management Studio, but it stopped working in that sense that NHibernate in my asp.net solution fired some connection exceptions. I just detached it from Management Studio because I couldn't open the database in there either.
But when I try to reattach it I get this error:
Attach database failed for Server 'MyPCName\SQLECPRESS'. (Microsoft.SqlServer.Smo) Unable to open the physical file "C:\Program
Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Whist.mdf".
Operating system error 32: "32(process cannot access the file because
it is being used by another process)". (Microsoft SQL Server, Error:
5120)
I have googled it but can't find a solution that works for me. E.g.:
http://www.byteblocks.com/post/2010/01/06/Unable-to-open-the-physical-file-Microsoft-SQL-Server.aspx
"Cannot open user default database. Login failed." after installing SQL Server Management Studio Express
There is something about User Instance but I cant figure out if it have anything to do with that:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
I have tried to delete the mdf file in windows but I can't do that either:
The action can't be completed because the file is open in system
I have tried stopping the SQL Server process, closing explore and delete from cmd and rebooting...
Hope someone can help - I guess it is just a simple thing..
I think you are on the right track. The error messages you are getting indicate that some SQL Server process is keeping the database file open. And SQL Server is designed to only allow the database file to be open by a single SQL Server process at a time.
You are also probably right to suspect User Instance of SQL Server Express to be the guilty process here. User Instance is a special SQL Server process that doesn't run as a service and instead is started and owned by the application that tries to connect to it. In your case there are two User Instances that can be the problem, one owned by the ASP.NET application you mentioned and one owned by you and started by Visual Studio. If you are using IIS Express or you configured the IIS App Pool for the ASP.NET application to run as your account then we only have one User Instance to worry about.
My recommendation is that you don't use User Instances and don't try to connect directly to the database file. Go over your connection strings and remove User Instance=true from all of them. You want to remove AttachDBFilename=... property as well.
If the database file is added to any of your Visual Studio solutions remove it (just be careful not to delete the file itself). The last step is to remove any connections to the database file from your Data Connections node in the Server Explorer.
Then restart the machine to make sure any User Instances that were running in the background were shutdown. After the machine starts again you should be able to attach the database file back to your main SQL Server Express using SQL Server Management studio. Let's name the database Whist
To connect to your database use the following connection string:
Data Source=.\SQLExpress;Integrated Security=true;Initial Catalog=Whist
When you connect from the ASP.NET application, make sure that you have created an account in your SQL Server Express for the application.
Turned out to be an issue with Daemon tools for me as well associating the .mdf file as an image.
If you want to copy your .mdf and .ldf file from your database, you can set your database file to 'offline' first . Please follow this steps:
Open your SQL Server 2008(suppose you are using SQL Server 2008 ).
Select your Database that you want to copy then left click.
Go to Tasks -> Take Offline.
Right click.
Then you can copy this database file to any folders.

sql exception when transferring project from usb to c:\

I'm working on a C# windows program with Visual Studio 2008. Usually, I work from school, directly on my usb drive. But when I copy the folder on my hard drive at home, an sql exception is unhandled whenever I try to write to the database. it is unhandled at the conn.Open(); line. here's the exception unhandled
Database 'L:\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' as database 'PatientMonitoringDatabase'.
it's weird, because my connection string says |DataDirectory|, so it should work on any drive... here's my connection string:
string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
"Initial Catalog=PatientMonitoringDatabase; " +
"Integrated Security=True";
I think the complaint here is that your local SQL Server Express install already has a
PatientMonitoringDatabase attached and running.
Connect to localhost with SQL Server Management Studio Express (if not installed, download here) and remove/detach the existing PatientMonitoringDatabase database. Whether it's a persistent database or only active within a running application, you can't have 2 databases with the same name at the same time attached to a SQL Server instance.
The second exception you get because the SQL Server Express instance doesn't have permission to open the database file. What are the permissions on the file, and what is SQL Server running under?
problem solved. got lucky on that one. I got a hint by browsing microsoft forums. The hint led me to open my "SQL Server Configuration Manager", and I had to do something with "SQL Server (SQLEXPRESS)". So I double clicked on it, and changed the "Built-in Account" from "Network Service" to "Local System". and now it works.... I got lucky i guess...
but, you do have to detach the database. So for that, thx Nick Craver