Thứ Năm, 18 tháng 9, 2014

Cách thêm một bản ghi mới vào trong một bảng dữ liệu có sẵn

Ví dụ: Bảng User( ID,Name,Age)
1. Câu lệnh thực thi thêm dữ liệu
INSERT INTO User VALUES( '1234','tên của bạn',20)
Hoặc 
INSERT INTO User( ID,Name,Age) VALUES( '1234','tên của bạn',20)
 
2. Câu lệnh lấy kết quả vừa thêm mới
 SELECT  ID,Name,Age FROM User ;

WITH common_table_expression (Transact-SQL)

Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.

[ WITH <common_table_expression> [ ,...n ] ]

<common_table_expression>::=
    expression_name [ ( column_name [ ,...n ] ) ]
    AS
    ( CTE_query_definition )
 
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
AS
-- Define the CTE query.
(
    SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
    FROM Sales.SalesOrderHeader
    WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO 

 

Lấy giá trị của một radio xem đang ở trạng thái check hay chưa check

<input type="radio" id="chktest" name="test"/> 
<script>
if ($('[name="test"]').is(':checked'))
</script> 

Thứ Tư, 17 tháng 9, 2014

chuyển đổi từ kiểu chuỗi sang kiểu số - và check xem chuỗi truyển vào chỉ là số hay không

int num;
bool isNumeric = int.TryParse(txtNum.text, out num);
if(isNumeric)
{
// Do your code, the value is number
}
else
{
// value is not a number
}

Thứ Tư, 30 tháng 7, 2014

chuyển từ chuỗi sang datetime

string dateTime = DateTime.Now.ToString("yyyy-MM-dd h:mm tt");   

DateTime dt = DateTime.Parse(dateTime);

Thứ Ba, 29 tháng 7, 2014

Lấy các ngày trong tuần sử dụng biến DateTime C#

using System;

class Program
{
    static void Main()
    {
 // Get currrent day of week.
 DayOfWeek today = DateTime.Today.DayOfWeek;
 Console.WriteLine("Today is {0}",
     today);

 // Test current day of week.
 if (today == DayOfWeek.Monday)
 {
     Console.WriteLine("DO WORK");
 }

 // Demonstrate all DayOfWeek values.
 Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
     DayOfWeek.Monday,
     DayOfWeek.Tuesday,
     DayOfWeek.Wednesday,
     DayOfWeek.Thursday,
     DayOfWeek.Friday,
     DayOfWeek.Saturday,
     DayOfWeek.Sunday);
    }
}

Output

Today is Monday
DO WORK
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

Thứ Bảy, 26 tháng 7, 2014

Thêm dữ liệu quan hệ giữa hai bảng trong C# - Add Relations

In a DataSet with multiple DataTable objects, you can use DataRelation objects to relate one table to another, to navigate through the tables, and to return child or parent rows from a related table.
The arguments required to create a DataRelation are a name for the DataRelation being created, and an array of one or more DataColumn references to the columns that serve as the parent and child columns in the relationship. After you have created a DataRelation, you can use it to navigate between tables and to retrieve values.
Adding a DataRelation to a DataSet adds, by default, a UniqueConstraint to the parent table and a ForeignKeyConstraint to the child table. For more information about these default constraints, see DataTable Constraints.
The following code example creates a DataRelation using two DataTable objects in a DataSet. Each DataTable contains a column named CustID, which serves as a link between the two DataTable objects. The example adds a single DataRelation to the Relations collection of the DataSet. The first argument in the example specifies the name of the DataRelation being created. The second argument sets the parent DataColumn and the third argument sets the child DataColumn.

customerOrders.Relations.Add("CustOrders",
  customerOrders.Tables["Customers"].Columns["CustID"],
  customerOrders.Tables["Orders"].Columns["CustID"]);