Select random các records từ database

by code2code 28. January 2010 15:07

Đôi khi lập trình bạn vẫn thường gặp trường hợp phải xử lý vấn đề trên. Ví dụ: Lấy ngẫu nhiên danh sách câu hỏi sử dụng cho chương trình thi trắc nghiệm.

Rất nhiều bạn chọn giải pháp là xử lý dữ liệu đã được lấy lên từ database.
Hôm nay mình giới thiệu các truy vấn các câu hỏi từ database:

Cách thứ nhất : dùng hàm Rand() hàm này sẽ trả về giá trị ngẫu nhiên > 0 và < 1.

Nhưng xử dụng hàm này có một bất lợi: khi bạn sử dụng trực tiếp hàm này trong câu lệnh truy vấn sẽ tạo ra một giá trị giống hệt nhau cho tất cả các records. Câu lệnh truy vấn sau :

SELECT TOP 5 ProductName, RAND() as RandomValue FROM products ORDER BY 2
SELECT RAND()


Returns result 0.7154366573674853

sẽ cho ra kết quả:

ProductName RandomValue
Alice Mutton 0.80020779549998178
Aniseed Syrup 0.80020779549998178
Boston Crab Meat 0.80020779549998178
Camembert Pierrot 0.80020779549998178
Carnarvon Tigers 0.80020779549998178

Các giải quyết là viết một funciton sử dụng hàm rand này.

Cách thứ 2: Sử dụng hàm NewID(). Hàm này khắc phục vấn đề gặp ở trên với hàm rand(). Khi bạn sử dụng trực tiếp hàm NewID trong câu lệnh select thì kết quả sẽ tạo ra các giá trị ngẫu nhiên hoàn toàn khác nhau.

SELECT TOP 5 productName FROM products ORDER BY NEWID()


kết quả chạy lần thứ nhất :

* Guaraná Fantástica
* Tunnbröd
* Ikura
* Filo Mix
* Genen Shouyu

Kết quả chạy lần thứ 2:

* Mozzarella di Giovanni
* Scottish Longbreads
* Gustaf's Knäckebröd
* Louisiana Fiery Hot Pepper Sauce
* Filo Mix

Tags: ,

SQL

Microsoft SQL Server Migration Assistant for MySQL v1.0 CTP1

by Neon Quach 13. January 2010 02:04

Microsoft has made available for download a free tool designed to streamline the migration of MySQL databases to SQL Server. At the start of this week, the company began offering customers a preview version of Microsoft SQL Server Migration Assistant. According to the software giant, the Community Technology Preview of SQL Server Migration Assistant (SSMA) for MySQL v1.0 integrates seamlessly with not only SQL Server 2005, but also SQL Server 2008 and even SQL Azure. The company underlines that the migration assistant is capable of automating aspects of the migration process to SQL Server 2005 & 2008 and to SQL Azure Database from MySQL.

“SQL Server Migration Assistant for MySQL is the newest migration toolkit, others include Oracle, Sybase, Access, and an analyzer for PowerBuilder. The toolkits were designed to tackle the complex manual process customers deal with when migrating databases. In using the SQL Server Migration Assistants, customers and partners reduce the manual effort; as a result the time, cost and risks associated with migrating are significantly reduced. Our recent SSMA survey showed that 94% of SSMA downloaders would recommend the tool to others,” revealed Erika Sommer, a member of Microsoft's SQL Server marketing team.

The Microsoft SQL Server Migration Assistant is current up for grabs as a free download from Microsoft, and because the release is a CTP build, the fact that the company is not charging customers anything for the tool is understandable. However, the promise from the software giant is that the SQL Server Migration Assistant will continue to be free even after general availability in mid-2010.

“Since the SQL Server Migration Assistants for Oracle became available in June of 2005, Microsoft has seen more than a 250,000 downloads across all of the SSMAs. Motorola, Simon & Schuster, Sony Ericsson and Artesia are just a few of the customers that have downloaded and used the SQL Server Migration Assistant toolkit to move to Microsoft SQL Server,” Sommer added.

Microsoft SQL Server Migration Assistant 2008 for MySQL v1.0 CTP1 is available for download here

Microsoft SQL Server Migration Assistant 2005 for MySQL v1.0 CTP1  is available for download here.

Required:

  • Microsoft Windows Installer 3.1 or a later version.
  • The Microsoft .NET Framework version 2.0 or a later version.
  • MySQL Connector/ODBC v5.1.
  • 1 GB RAM.


Tags: , , ,

mysql | SQL

SQL Azure Explorer Addin for VS2010 Beta 1

by Neon Quach 1. October 2009 03:02

Johan Danforth and Dag König just released the alpha version of a (so far) small Codeplex project we’ve been working on for the last week or so – an SQL Azure Explorer Addin for Visual Studio 2010 Beta 1.

The reasons for this project are several, but mostly to learn more about VS2010, how to create addins for it, dig into SQL Azure and at the same time learn some WPF and XAML.

The addin is a VSIX package and easily installed in VS2010 by just downloading and double clicking it.

Here’s a sample screenshot of the explorer and the integrated SQL Editor:



If you got VS2010 Beta 1 and some SQL Azure databases you work with, please download and feedback. I’m sure Microsoft will eventually support SQL Azure in the built-in server explorer, but as I said, this is for fun (it’s great fun to code addins) and the code might be useful for other future projects :)

Reference

Tags:

SQL | Tools | Visual Studio 2010 | Visual Studio Add-In

Publish CSDL cục bộ sử dụng Data Publish Wizard

by Neon Quach 28. September 2009 02:48

Publish CSDL cục bộ sử dụng Data PublishWizard

Giới thiệu

Như 1 người phát triển web, mình hay làm việc với dử liệu trên local (máy cục bộ) trước khi đưa chúng lên mạng (go live), và 1 trong những giai đoạn cuối của việc test trên local là đưa database cũng như data của nó lên host.

Mình hay dùng tiện ích Data Publish Winzard, 1 tiện ích mà khi cài Visual Studio 2008.

Cách thực hiện

Note: Data Publish Winzard có thể cài đặt riêng lẻ phiên bản chạy độc lập.

1. Khởi động VS
2. View 
Server Explorer
3. Data Connection 
Add Connection



4. Chọn Provider là Microsoft Sql Server.
5. Add Northwind Connectin



6. Chọn database 
Publish to provider






7. Chọn database Northwind



Uncheck Script all objects in the selected database, nếu muốn tùy chọn các đối tượng được publish.

8. Chọn location



9. Chọn schema option



Chú ý: type of data to publish ở đây có 3 tùy chọn:
- Data only: chỉ publish data (generate insert script)
- Schema and data: Cấu trúc database và dữ liệu.
- Schema only: chỉ publish cấu trúc.

Next 
finished



Xong, giờ thì các bạn có thề dùng sql script này chạy trên database server.



Reference

Tags: ,

SQL | Visual Studio .NET

Microsoft WebSite Spark

by Neon Quach 25. September 2009 04:24

Microsoft supporting web startups and independent web developers by providing free software's for 3years

Microsoft has launched today a very exciting program called WebSiteSpark for individual developers and small web startup companies having less than 10 employees.

Given below are few software licenses which will be given free to you for 3 years

  • 3 licenses of Visual Studio 2008 Professional Edition
  • 1 license of Expression Studio 3 (which includes Expression Blend, Sketchflow, and Web)
  • 2 licenses of Expression Web 3
  • 4 processor licenses of Windows Web Server 2008 R2
  • 4 processor licenses of SQL Server 2008 Web Edition
  • DotNetPanel control panel (enabling easy remote/hosted management of your servers)

IMHO it is very good initiative by Microsoft to promote small companies and individuals to leverage their products and build next generation application on the web.

More information can be found here

http://weblogs.asp.net/scottgu/archive/2009/09/24/announcing-the-websitespark-program.aspx

http://www.microsoft.com/web/WebSiteSpark

 

Tags:

General | SQL | Visual Studio .NET

IIS Database Manager Release Candidate

by Neon Quach 22. September 2009 04:28

IIS Database Manager Release Candidate



Brief Description
IIS Database Manager allows you to easily manage your local and remote databases from within IIS Manager.

Overview

IIS Database Manager allows you to easily manage your local and remote databases from within IIS Manager. IIS Database Manager automatically discovers databases based on the Web server or application configuration and also provides the ability to connect to any database on the network. Once connected, IIS Database Manager provides a full array of management options including managing tables, views, stored procedures and data, as well as running ad hoc queries.
IIS Database Manager provides native support for SQL Server and is also fully extensible for developers to add support for other database systems. In addition, because IIS Database Manager is an extension of IIS Manager, administrators can securely delegate the management of databases to authorized local or remote users, without having to open additional management ports on the server.

IIS Database Manager Release Candidate (x86)


IIS Database Manager Release Candidate (x64)

Tags:

SQL

Chèn nhiều dòng trên 1 câu lệnh SQL 2008

by Neon Quach 17. September 2009 17:16

Chèn nhiều dòng trên 1 câu lênh SQL 2008 Nếu bạn cố gắng thực thi câu lệnh này trên sql 2005 thì sẻ báo lổi error syntaxt, thế như nếu bạn thử chạy câu lênh này trên sql 2008 thì thành công.

INSERT INTO [Northwind].[dbo].[Region] ([RegionID] ,[RegionDescription])

VALUES(5,'Mien Nam Viet Nam'),(6,'Mien Bac Viet Nam') GO

SELECT * FROM Region;

 

Tags:

SQL

Cấu hình Mail và gửi mail trong Sql 2008

by Neon Quach 12. September 2009 04:07

Cấu hình Mail và gửi mail trong Sql 2008

 

Để gửi mail sử dụng Database Mail trong Sql Server cơ bản gồm 3 bước:

1. Tạo tài khoản và hồ sơ (profile)

2. Cấu hình Email.

3. Send Email

 

Bước 1. Tạo tài khoản và hồ sơ (profile)

 

Mở Sql Server 2008 chọn Database Mail trong Management node, và chọn Configure Database Mail

 

sql server 2008

 

database mail configuration wizard

 

 

sql 2008

 


 

 

 

 

sql 2008 send mail

 

sql 2008 send mail

 

sql 2008

 

Bước 2. Cấu hình Email.

 

Sau khi đả cấu hình hồ sơ và tài khoản thành công, bạn phải cấu hình database mail tiếp, tức là bạn phải bật tham số Database Mail XPs thông qua stored procedure sp_configure.

 

sp_CONFIGURE 'show advanced', 1

GO

RECONFIGURE

GO

sp_CONFIGURE 'Database Mail XPs', 1

GO

RECONFIGURE

GO

 

 

Bước 3. Gửi Email

Sau khi đả hoàn tất công việc cấu hình, bây giờ chúng ta sẳn sàn để gửi mail, để gửi mail chúng ta phải thực thi 1 stored procedure sp_send_dbmail và cung cấp các tham số được yêu cầu như bên dưới:

 

USE msdb

GO

EXEC sp_send_dbmail @profile_name='Quach Nguyen',

@recipients='nguyenqnh@infoworldstudent.com',

@subject='Test message',

@body='This is the body of the test message.

Congrates Database Mail Received By you Successfully.'

 

 

Sau khi đả nhập các tham số yêu cầu xong, hãy chắc chắn rằng stored procedures đả được thực thi, và mail được xếp hàng (queued) bởi Service Broker.

 

Database mail sẻ sao chép thông điệp được gửi đi và hiển thị chúng trong sysmail_allitems, sysmail_sentitems, sysmail_unsentitems, sysmail_faileditems. Trạng thái của mail được lưu trong bảng sysmail_mailitems, khi mail được gửi thành công thì trường send_status sẻ là 1, nếu thất bại sent_status sẻ là 2, còn chưa gửi sẻ là 3.

 

Bạn có thể kiểm tra như bên dưới:

 

SELECT *

FROM sysmail_mailitems

GO

SELECT *

FROM sysmail_log

GO

 

 

Trạng thái có thể được kiểm tra bởi việc sử dụng bảng sysmail_sentitems.

 

 

Sau khi gửi mail bạn kiểm tra mail trong hộp thư mail, như mình đả nhận được mail này.

 

 

Best,

QuachNguyen

Tags:

SQL

Liệt kê danh sách tên Schema và tên table trong Database

by Neon Quach 20. June 2009 04:01

Hôm trước mình đi phỏng vấn và anh kỹ thuật hỏi đến phần sql trong đó có 1 yêu cầu là tìm danh sách schema (lược đồ) và tên table trong 1 database, mình rất ấp úng cho câu hỏi này, vì mình là "thấn dân" của web developer nên cũng hạn chế.

Sau đó mình tìm kiếm trên mạng cuối cùng cũng được câu query này. Nay mình muốn chi sẻ với mọi người.

SELECT QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name)
AS SchemaTable FROM sys.tables


Hope this help,

Tags:

SQL

Calling SQL Server stored procedures from Microsoft.NET

by Neon Quach 21. May 2009 23:42

Calling SQL Server stored procedures from Microsoft.NET

Giới thiệu :

Mình cho rằng người đọc phải biết cách làm thế nào để viết 1 thủ tục (stored procedure) trong SQL Server . Quyết tâm của minh làm diễn tả các cách khác nhau của việc gọi 1 stored procedure. Và mình cố gắng diễn đạt 1 cách thật đơn giản.

Nền tảng Cơ bản :

Mình muốn tạo 1 trang asp.net mà sẻ truyền các tham số vào 1 stored procedure để xử lý dử liệu vào 1 cở sở dử liệu nào đó. Mình rất ngạc nhiên rằng là mình không thể nào tìm thấy 1 thí dụ trực tuyến nào thông qua google. Đó cũng chính là động lực thúc đẩy mình viết bài viết này .

Việc sử dụng đoạn mã :
Đâu tiên mình muốn cho các bạn thấy là làm như thế nào mà chúng ta có thể gọi 1 stored đơn giản và không có tham số đầu vào. Để làm được điều đó CSDL của bạn phải có 1 stored đơn giản được đặt tên là SP1. Cho nên chúng ta có thể gọi stored bằng cách này :



Và bây giờ mình muốn cho các bạn thấy 1 thí dụ làm thế nào chúng ta có thể gọi 1 stored với tham số đầu vào. Đặt là @Param1 cho tham số đầu tiên và @Param2 cho tham số thứ 2. Có thể xem đoạn mã sau :



Trong cách ở trên chúng ta có thể gọi stored với tham số. Nếu 1 ai đó khảo xát đoạn mã trên thì họ sẻ thấy rằng mình đả gắn tham số đầu tiên vào và kế đến là tham số thứ 2, cái mà không có hiệu quả nếu họ nghỉ về việc dùng lại.

Bây giờ sự lo lắng của mình là làm thế nào mình viết 1 phương thức mà nó thực thi 1 stored và có thể dùng lại được . Cách tiếp cận cùa mình là trong mổi ứng dụng chúng ta sử dụng các lớp tiện ích chung. Có lẻ là utility/DB utility, cho nên bạn có thể viết 1 class tỉnh DB utility có thề đặt tên là ExecSP:



Phương thức trên nhận vào 3 đối số , cái đầu tiên là tên của stored, cái thứ 2 là thêm của tham số cần gọi, cái còn lại là giá trị tương ứng của nó.

Bây giờ mình sẻ cho các bạn thấy làm thế nào để gọi phương thức đó từ UI (Users Interface) .



Cũng như bạn có thề gọi phương thức tình trên mà không có tham số truyền vào như sau :



Chú ý : Chuổi kết nối các bạn nên lưu vào File Web.config. Và nên sử dụng using block khi khởi tạo các đối tượng.

Happy programming!

Tags:

SQL | Visual Studio .NET | C#

Powered by BlogEngine.NET 1.6.0.0 - Eco Theme by n3o Web Designers