Connect to database in App_Data folder - sql-server

Is it possible to connect to a database in the App_Data project folder using the command line? I'm trying to test some SQL statements outside of the VS environment.
I tried using the below, but that didn't seem to work:
sqlcmd -S .\Sqlexpress -d [location of mdf]

Not sure about the app_data folder, but if you have the db files running off of a sql install, you can use sqlcmd command line which is installed with the sql client tools or the sql feature pack (sql native client).

Related

Run sqlserver to restore database on docker (bitbucket)

I want to mount an sql server on Bitbucket so I can download a db (bak file), restore it an upload it later on a main scheme on my server. I've tried:
Mount an SQLServer image, but didnt recognize the path to the downloaded file (problems with mssql tools)
Mount an SQLtool image, but didnt recognize the path too
Python 3.8 image and install sql server and sql tool, but systemctl wasnt found
How can I do this?

PowerShell v5.1 - Loading SqlServer Module does not load Read-SqlTableData or Write-SqlTableData, but Loads All Other Commands

On my Windows Server 2012 R2 server, the same server as my SQL Server service instance is running, I had upgraded to PowerShell v5.1. I proceeded to load the SqlServer module v21.1.18121. All commands loaded fine except for Read-SqlTableData and Write-SqlTableData. The darndest thing! When I load the same SqlServer module on my Win10 development box, all commands are present. I suspect something like a version problem with the available .Net framework or some such, or a collision with SQL Server's modules. Anyone with thoughts on this one?
The SqlServer module comes with the SQL Server installation and thems to be in a different version.
Module location on a SQL Server
(Get-Module -l SqlServer).path
-> C:\Program Files\WindowsPowerShell\Modules\SqlServer\SqlServer.psd1
Module location from PowerSehll gallery
(Get-Module -l SqlServer).path
-> C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.0.17279\SqlServer.psd1
You can enforce PowerSell to load a module from specific location
Install-Module -Name SqlServer
Import-Module -Name (Get-Module -l SqlServer).path -Verbose
I've just encountered the same problem. I've tried many things, but I think the problem stems from having the module SQLPS installed alongside SqlServer module. (SQLPS being an older version of sqlserver).
The steps I used: (probably a little excessive)
Close every application that may have a reference to Powershell
modules. For me this list included Visual Studio, SSMS, SQL Agent,
and of course all Powershell cmd and/or IDE.
Open a Administrator PowerShell command shell.
execute:
Uninstall-Module sqlps
Uninstall-Module sqlserver
Check the Powershell modules folder to make sure you've cleared the modules.
The place to look is:
ls "c:\program files\WindowsPowerShell\Modules"
Both of the folders "sqlps" and "sqlserver" should be missing or empty.
Now re-install sqlserver module:
Install-Module sqlserver
Check that you have the Write-SqlTableData:
Get-Command -module sqlserver
In case you suspect you have an error with versioning: I have tried and tested sqlserver module versions: 21.1.18218 & 21.0.17099

Powershell sql server module 2017 manual install

Just installed sql server 2017 RTM. After that I installed SSMS version 17. Funny enough this won't install the powershell module but you have to install it from PowerShell Gallery. Now my servers of course (being an enterprise environment) have no direct connection to the internet and not even the management computers in that domain have. From the Powershell Gallery website I was not able to manually install the powershell module. Question is do anybody here managed to have latest sql server powershell module installed for sql server 2017?
I'm not in an environment to test this but one method would be to install the module on a computer/server that does have access to the internet, download and install it there, locate the module, and then copy it to the other server and import it. So, something like:
On the other computer (with internet access):
Install-Module -Name SqlServer
(Get-Module -ListAvailable SqlServer).Path
Go to the path and copy the file(s) to the same location on the other server. Then, on the other server, and run:
Import-Module -Name SqlServer
This assumes that both hosts are running the same version of Powershell; or at least compatible ones.

How to import a shape file into SQL Server?

In the past I have used shape2sql to import shape files into SQL Server.
I tried that route again on a newer Windows 10 box, and nothing seems to happen when trying to use that application.
Any suggestions for either getting shape2sql to work, or alternative ways to upload shape files into SQL Server?
So, it turns out that ogr2ogr has the ability to import directly into a SQL Server database now. I created a temporary database and did the following:
Install ogr2ogr via the GDAL core MSI installer from http://www.gisinternals.com/release.php
Open a command line to the installed path
Run the following:
ogr2ogr -overwrite -f MSSQLSpatial -lco “GEOM_TYPE=geography” -a_srs
"EPSG:4326"
"MSSQL:server=.;database=temp_import;trusted_connection=yes" "[path to
shape file]"
I've managed to import successfully on remote server. Here goes procedure
Install QGIS Desktop 3.10.5..or whatever version is actual.
How to find out what goes to parameter -a_srs, in my casa that is : -a_srs "EPSG:3765":
Open QGIS Desktop 3.10.5, open new project and drag-drop your shape file to Layers left sidebar. Right click on that layer and go to Properties. There you will be able to read your -a_srs information.
Run command in CMD:
C:\Program Files\QGIS 3.10\bin>ogr2ogr -progress -f "MSSQLSpatial" "MSSQL:server=xxx.xxx.xxx.xxx,yyyy;database=yourDB;UID=yourUse;PWD=yourPassword" "C:\Users\Username\Desktop\ShapeFiles\Name_of_shape_file.shp" -a_srs "EPSG:3765" -lco PRECISION=NO
Where xxx.xxx.xxx.xxx - IP address of your server
yyyy - port number of your server if you are running SQL Server on different port than standard (I think you can omit that in case you are)
Output:
0...10...20...30...40...50...Warning 1: Ring Self-intersection at or near point 664451.64629999956 5053409.8562000087
60...70...80...90...100 - done.

Deploy the database to Docker Container microsoft/mssql-server-linux

I have a database running on SQL Server (13.01) on Windows. I like to deploy it to the Docker Container on Linux using SSDT.
I can perfectly connect to the server running on Docker and create/drop database manually and play with the data.
The problem is I can not publish it. I'm executing following script on Powershell
PS: SqlPackage.exe /Action:Publish /SourceFile:"d.dacpac" /TargetConnectionString:"server=containeraddress;database=thedatabase;user id=sa;password=thepassword;
and getting the following error.
Unable to connect to master or target server 'the database'. You must have a user with the same password in master or target server 'the database'. (Microsoft.Data.Tools.Schema.Sql)
I have the same user and same password on target and source servers.
Is there anybody has the same problem and know how to solve it?
I'll post this here as most of the answers refer to having an existing compiled dacpac file, which may not always be possible. I haven't seen similar ideas posted elsewhere to the solution I'm suggesting here.
Given your usage of docker and if you wish to compile your visual studio project inside the container, given certain combinations of the container base OS and image may not be possible to create a dacpac file with msbuild.
You can work around restoring the database using a series of unix based commands, taking note that the visual studio database project is usually just a series of SQL files, below I show an example of this, where I concat the SQL files into a single file and call sqlcmd to run the script;
FROM mcr.microsoft.com/mssql/server
WORKDIR /init
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=MyPassword
EXPOSE 1433:1433
RUN apt-get update && apt-get install dos2unix
COPY /solution_folder/database/Tables/*.sql /init/
WORKDIR /database
RUN echo "CREATE DATABASE [database_name];\nGO\nUSE [database_name];\n” >> /database/create.sql
RUN for f in /init/*.sql; do dos2unix $f; cat $f >> /database/create.sql; echo "\nGO\n" >> /database/create.sql; done
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ‘MyPassword’ -i /database/create.sql && pkill sqlservr
The reason for "dos2unix" is that the SQL files created within visual studio have unique hidden cr/lf (and other characters) which the linux version of sqlcmd won't interpret successfully and will cause errors (which is kind of bizarre and this is exactly the kind of thing you'd want a cross platform database to be able to cope with)
Also, within the final run command you have to start-up the sql server service temporarily otherwise you'll also get errors; it's a little bit of work-around, and a bit fiddly and I'm not sure entirely that the microsoft sql server linux container is well designed enough for the simple task of restoring a database like this, the nuances are the differences between building and running a container and needing some sort of happy middle ground of both concepts for it to work.
Given here isn't a complete solution to restore, it only deals with Tables from the project file, although it should be trivial to expand to scalar functions and stored procedures.
Which version of SqlPackage.exe are you using? Only the most recent release candidate versions of SqlPackage.exe support SQL Server vNext CTP. The SqlPackage release candidate can be downloaded here: https://www.microsoft.com/en-us/download/details.aspx?id=54273

Resources