Edi Wang

.NET and Azure Developer

SQL Server

PowerShell to Enable SQL Server Express Remote Access

SQL Server Express is a free edition of Microsoft's SQL Server, which is a relational database management system (RDBMS). However, remote access is not enabled by default on this SKU. To access a SQL Server Express instance from the network, we have to do a few steps. There's plenty of guide on the internet telling you how to enable remote access via GUI, but I would like to do it from PowerShell. …
SQL Server PowerShell

Automated Deployment Script with Azure CLI and PowerShell Core

My blog project, "Moonglade", is optimized for running on Microsoft Azure. It can combine up to 14 Azure services together for a full deployment, manually setup is difficult and takes time. I chose Azure CLI with PowerShell Core to write a deployment script that enables a quick on-board experience to setup Moonglade in 10 minutes with minimal Azure Services. Let's see how it is done. …
SQL Server Azure App Service

Import Data to Azure Storage Table from SQL Server

Recently, there was a demand to switch data storage from SQL Server database to Azure Storage Table. However, neither SSMS nor Azure Portal provide a direct import function. You may now consider writing a tool to import data, but actually it isn't necessary. I still managed to do it within mouse clicks. But the first thing to warn everyone is that relational databases like SQL Server are not …
SQL Server Azure Storage

SQL Server Export Data Tier Application via Command Line

We can use SSMS to export data-tier application for an SQL Server database. I've been using this method for several years on my local machine. However, to make things work under automated environments, like CI/CD environments, or some automated scheduled tasks. We usually need to do it under the command line.  Microsoft has provided a cross-platform command-line tool that can import/export DAC: …
SQL Server DAC

Merge Data for N-N Relationship Tables in T-SQL

最近在整理博客的数据,需要做一个操作就是合并文章的分类。我的博客中文章和分类是多对多的关系。即一篇文章可以属于多个分类,一个分类可以包含多篇文章。这是一个很典型的多对多关系,我用的是一个多对多的表,做联合主键关联这些数据。 就像这样: 我需要做的是把“DotNetBeginner”这个分类的文章移到“CSharpAndDotNet”分类里去。但是因为原先在“DotNetBeginner”里的文章有些也是属于“CSharpAndDotNet”的,所以直接Update关联表的话,会产生重复的联合主键,就会爆。 直观一点看,写个SQL语句查询出原分类(DotNetBeginner)和目标分类(CSharpAndDotNet)中的数据: DECLARE @SourceCatId AS UNIQUEIDENTIFIER, @TargetCatId AS …
SQL SQL Server

图解:如何将SQL Azure数据库备份到本地SQL Server

用了两天SQL Azure后,发现SQL Azure数据库没有办法做传统意义上的“bak”备份,但我们确实很需要能够随时将数据库备份到本地,以更灵活的使用。所以今天就开荒了一下操作方法,并分享给大家。这样一来,我们既可以把SQL Server迁移到SQL Azure,又可以反过来把SQL Azure撸回SQL Server,这是一个比较完整的装逼过程,妹子们一定会赞不绝口。 步骤如下: 1. 启动SSMS2012,在目标SQL Azure数据库上点右键,选择“Export Data-tier Application”。 2. 在Export Settings选项卡里制定本地的备份路径,现在已经是“bacpac”拓展名的了。。。 3. 然后点击Next,确认后就向导就开始撸了。 4. 撸完以后会显示Operation Complete的消息,现在本地备份文件已经创建完成了。 5. 在 …
SQL Server SQL Azure Azure

Store and Read Images via SQL Server in ASP.NET MVC 3

First of all, I don't recommend storing images in the database, especially in a web project. Because file systems can provide static file access and get many benefits such as performance. Storing images in the database has certain limits. But if you must store images into SQL Server, here's how to do it.  SQL Image files are in fact, binary data. SQL Server provides us the image type for …
ASP.NET MVC SQL Server

SQL Server 授权传递的测试

上周公司有个妹子问了我一个关于SQL Server授权的问题,我没怎么玩过授权,不是很清楚。问题是这样的:如果用户A授权给用户B,用户B再授权给用户C,那么回收A的权限会不会把C的一起回收了。 我以前玩的都是在SQL Server中,给一个数据库添加Windows或者AD帐号,他们都是sysadmin角色,所以删除某个帐号并不影响别的帐号访问。这不适用于妹子的问题,所以今天我特地做了下测试。 测试内容: 用sa(SQL Server管理猿帐号)创建UserA, UserB。然后把SELECT News表的授权给UserA,再让UserA授权给UserB,最后回收UserA的权限,看看UserB还有木有权限。 首先,创建用户的脚本如下: -- Create a SQL Server login named UserA, and then creates a corresponding …
SQL Server

博客数据库升级手记(二):新数据库的建立及简单数据的导入

前面一片文章讲了如何将Access数据库直接导入SQL Server。但导入完成后,是强烈建议不要使用的。因为里面的object命名都与sql默认的不符,许多T-SQL行为也很诡异,所以我需要重新建立一个数据库,并且把旧的数据导入到新的数据库中。 新数据库的设计 对于新的数据库,这恰好是一次重构和升级的机会,我可以将4年前不合理的表结构设计重构一下(4年前也就是我第一次做这个博客的时候,菜鸟一只)。新的数据库中,博客子系统的设计如下(其实还有很多别的表,但不是给博客主业务用的,以后再讨论): 而旧数据库仅仅只有3张表:Blog、Category、Comment。 对于Category,没有太大的改动,仅仅是增加一个”Name”字段,用来做MVC的路由,而”DisplayName”的功能则和原先数据库是一样的,表示这个分类在页面上显示的名称。 Comment表增加了Email字段,是可选的 …
SQL SQL Server

博客数据库升级手记(一):将Access数据库迁移到SQL Server

本人使用的网站空间送了个200M的SQL Server2008 R2数据库,我不用白不用。所以上周花了很多时间把网站从Access和XML数据存储上迁移到了SQL Server数据库,顺便也升级了一下架构,目前博客子系统已基本完成迁移,最近将逐个迁移页面集系统、留言等模块。 在迁移的过程中,有许多值得记录的经验,在实际项目中非常实用,所以记录下来以备参考。 首先,我的迁移思路是:数据的迁移尽量不通过程序去做,应当采用SQL Server去实现。那么第一步我就得把Access数据库迁移到SQL Server上,这样才能在SQL Server中进行一致的管理和迁移到新的数据库上。 我原来的数据库是Access2007格式的(格式没有太大的关系,2003也可以这样操作)。在Access 2010中的操作方法如下: 1. 在Database Tools选项卡下选择“SQL Server”: 2. …
ACCESS SQL Server