Search
Thursday, April 3, 2008
what is ORM ? =.=
Object-Relational Mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" which can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
more detail: http://en.wikipedia.org/wiki/Object-relational_mapping
LinQ to SQL
before:
string query = “SELECT * FROM Products WHERE categoryId = 3”;
SqlCommand command = new SqlCommand();
command.CommandText = query;
there might be:
- runtime error in query... C# can't detect if we spell fron for FROM
Thus, with LINQ:
SomeDataContext database = new SomeDataContext();
List products = from p in database.Products
where p.Price > 300 && p.CategoryId == 3
select p;
OR with new C# 3.0 feature lambda expression '=>'
SomeDataContext database = new SomeDataContext();
Category aCategory = database.Categories.Single(c => c.Id == 7);
c => c.Id == 7
is lambda expression which means to get some c as category where id of c is 7
merging 2 tables each containing the same columns in sql
I have two tables, each containing the same columns: one containing 1000 rows and one with 800 rows. How can I create a third table which is a merge of the other two and ensuring there are no duplicates
===
Insert into table3
select * from table1
union
select * from table2
===
if there are no duplicate use: unionall
Wednesday, April 2, 2008
Unpacking Lists in a Table
problem example:
CREATE PROCEDURE get_product_names @ids varchar(50) AS
SELECT ProductID, ProductName
FROM Northwind..Products
WHERE ProductID IN (@ids)
above procedure will not work..
solution:
CREATE FUNCTION iter_charlist_to_tbl
(@list nvarchar(MAX),
@delimiter nchar(1) = N',')
RETURNS @tbl TABLE (listpos int IDENTITY(1, 1) NOT NULL,
str varchar(4000) NOT NULL,
nstr nvarchar(2000) NOT NULL) AS
BEGIN
DECLARE @endpos int,
@startpos int,
@textpos int,
@chunklen smallint,
@tmpstr nvarchar(4000),
@leftover nvarchar(4000),
@tmpval nvarchar(4000)
SET @textpos = 1
SET @leftover = ''
WHILE @textpos <= datalength(@list) / 2
BEGIN
SET @chunklen = 4000 - datalength(@leftover) / 2
SET @tmpstr = @leftover + substring(@list, @textpos, @chunklen)
SET @textpos = @textpos + @chunklen
SET @startpos = 0
SET @endpos = charindex(@delimiter COLLATE Slovenian_BIN2, @tmpstr)
WHILE @endpos > 0
BEGIN
SET @tmpval = ltrim(rtrim(substring(@tmpstr, @startpos + 1,
@endpos - @startpos - 1)))
INSERT @tbl (str, nstr) VALUES(@tmpval, @tmpval)
SET @startpos = @endpos
SET @endpos = charindex(@delimiter COLLATE Slovenian_BIN2,
@tmpstr, @startpos + 1)
END
SET @leftover = right(@tmpstr, datalength(@tmpstr) / 2 - @startpos)
END
INSERT @tbl(str, nstr)
VALUES (ltrim(rtrim(@leftover)), ltrim(rtrim(@leftover)))
RETURN
END
Implementation:
CREATE PROCEDURE get_company_names_iter @customers nvarchar(2000) AS
SELECT C.CustomerID, C.CompanyName
FROM Northwind..Customers C
JOIN iter_charlist_to_tbl(@customers, DEFAULT) s
ON C.CustomerID = s.nstr
go
EXEC get_company_names_iter 'ALFKI, BONAP, CACTU, FRANK'
strip out all non letter/number characters from a series of
I need to strip out all non letter/number characters from a series of strings in a query. Is there a better way to do this than a bunch of replace functions? /********************************* Removes any characters from @myString that do not meet the provided criteria. *********************************/ CREATE FUNCTION dbo.GetCharacters(@myString varchar(500), @validChars varchar(100)) RETURNS varchar(500) AS BEGIN While @myString like '%[^' + @validChars + ']%' Select @myString = replace(@myString,substring(@myString,patindex('%[^' + @validChars + ']%',@myString),1),'') Return @myString END Go Declare @testStr varchar(1000), @i int Set @i = 1 while @i < 255 Select @TestStr = isnull(@TestStr,'') + isnull(char(@i),''), @i = @i + 1 Select @TestStr Select dbo.GetCharacters(@TestStr,'a-z') Select dbo.GetCharacters(@TestStr,'0-9') Select dbo.GetCharacters(@TestStr,'0-9a-z') Select dbo.GetCharacters(@TestStr,'02468bferlki')
Tuesday, April 1, 2008
1900-01-01 00:00:00.000 bug
bug:
1900-01-01 00:00:00.000 entry in SQL
cause:
when you enter blank string into a datetime type field
the default value is 1900-01-01 00:00:00.000
fix:
don't insert empty/blank string into field that is a datetime type.
LINQ:
- LinQto Object : use of standard query operator (SQO) for pulling data from any object that can be implemented by interface IEnumerable. This query can be use for data in memory.
- LinQ to ADO.NET: LinQ to SQL , LinQ to dataset, LinQ to Entities
- LinQ to XML
hello word in LinQ:
class Program
{
static void Main(string[] args)
{
string[]helloWord={"hello","this","is", "LINQ","by","Philip","H" };
var results = from word in helloWord where(word[0]>'g' || (word[0]>'K'&& word[0]<'M')) select word+' ';
foreach(var wordItem in results) Console.Write(wordItem);
}
}
Subscribe to:
Posts (Atom)
