17 Ağustos 2009 Pazartesi

CASE (Transact-SQL)


Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END



input_expression

Is the expression evaluated when the simple CASE format is used. input_expression is any valid expression.

WHEN when_expression

Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and eachwhen_expression must be the same or must be an implicit conversion.

THEN result_expression

Is the expression returned when input_expression equals when_expression evaluates to TRUE, or Boolean_expression evaluates to TRUE. result expression is any valid expression.

ELSE else_result_expression

Is the expression returned if no comparison operation evaluates to TRUE. If this argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL. else_result_expression is any valid expression. The data types of else_result_expression and any result_expression must be the same or must be an implicit conversion.

WHEN Boolean_expression

Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.



Select Name, StandardCost, ListPrice, (ListPrice - StandardCost)/StandardCost * 100,

case

when (ListPrice - StandardCost)/StandardCost * 100 <>

when (ListPrice - StandardCost)/StandardCost * 100 between 50 and 99 then 'Not Bad'

else 'Superb'

End [Profit]

from Product


SELECT ProductNumber, Name, 'Price Range' =

CASE

WHEN ListPrice = 0 THEN 'Mfg item - not for resale'

WHEN ListPrice <>

WHEN ListPrice >= 50 and ListPrice <>

WHEN ListPrice >= 250 and ListPrice <>

ELSE 'Over $1000'

END

FROM Production.Product

ORDER BY ProductNumber


Select AddressID, AddressLine1,

case

When AddressLine1 like '%Street%' or AddressLine1 like '%St.%' or

AddressLine1 like '%Ave%' or AddressLine1 like '%Rue%' then 1

else 0

End IsStreet

from Address

How can i change the font and font size in Visual Studio 2008


Menu->Tools->Options->Fonts and Colors

Then you can make whatever you want!


14 Ağustos 2009 Cuma

Best Way convert C# DateTime format to a SQL DateTime

private void Test(DateTime startdate)
{

SqlConnection cn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");

DateTime enddate = startdate.AddDays(1);

SqlDataAdapter da = new SqlDataAdapter
(
@"SELECT Top 10 id ID, firstname Firstname , lastname Lastname, email Email, gamescore GameScore
FROM GameScores
WHERE createDate >= '" + startdate.ToString("yyyy-MM-dd 00:00:00") + "' and createDate < '" + enddate.ToString("yyyy-MM-dd 00:00:00") +
@"' ORDER BY gamescore desc", cn
);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}


//datetime.ToString("yyyy-MM-dd 00:00:00") is useful :)

11 Ağustos 2009 Salı

Veritabanı bağlantısı oluşturulamadı.

SQL Server veritabanını henüz oluşturmadıysanız, Web Sitesi Yönetim aracından çıkın, veritabanını oluşturmak ve yapılandırmak üzere aspnet_regsql komut satırı yardımcı programını kullanın, ardından sağlayıcıyı ayarlamak için bu araca dönün.


Hatasını aldığınızda Başlat->Programs->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 Command Prompt seçeneğine tıklayın.

Çıkan ekrana aspnet_regsql komutunu yazıp enter' a basın. Bir wizard ekranı karşınıza gelecektir. Burdan daha önceden oluşturmuş olduğunuz bir database' le bağlantı kurmak için gerekli ayarları yapabilirsiniz.

10 Ağustos 2009 Pazartesi

Xml dosyalarının .NET Ortamında C# ile Okunup, Yazılması Örnek 2



Örnek 2:

using System;
using System.Xml;
namespace TypeReader
{
class TypeReader
{
static void Main(string[] args)
{
int ws = 0;
int pi = 0;
int dc = 0;
int cc = 0;
int ac = 0;
int et = 0;
int el = 0;
int xd = 0;
// Dosya bitene kadar dökümanı oku
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// Node Tipi Declaration ise
if (nType == XmlNodeType.XmlDeclaration)
{
Console.WriteLine("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
// Node Tipi Comment ise
if (nType == XmlNodeType.Comment)
{
Console.WriteLine("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
// Node Tipi Attribute ise
if (nType == XmlNodeType.Attribute)
{
Console.WriteLine("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
// Node Tipi Element ise
if (nType == XmlNodeType.Element)
{
Console.WriteLine("Element:" + textReader.Name.ToString());
el = el + 1;
}
// Node Tipi Entity ise
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
et = et + 1;
}
// Node Tipi Process Instruction ise
if (nType == XmlNodeType.ProcessingInstruction)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
pi = pi + 1;
}
// Node Tipi Document ise
if (nType == XmlNodeType.DocumentType)
{
Console.WriteLine("Document:" + textReader.Name.ToString());
dc = dc + 1;
}
// Node Tipi Boşluk ise
if (nType == XmlNodeType.Whitespace)
{
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
}
// Sonucu yaz;
Console.WriteLine("Toplam Aciklama Satiri: " + cc.ToString());
Console.WriteLine("Toplam Öznitelik: " + ac.ToString());
Console.WriteLine("Toplam Eleman: " + el.ToString());
Console.WriteLine("Toplam Varlik: " + et.ToString());
Console.WriteLine("Toplam Islem Talimati: " + pi.ToString());
Console.WriteLine("Toplam Bildirim: " + xd.ToString());
Console.WriteLine("Toplam Döküman Tipi: " + dc.ToString());
Console.WriteLine("Toplam Bosluk: " + ws.ToString());
}
}
}

Xml dosyalarının .NET Ortamında C# ile Okunup, Yazılması Örnek 1

Örnek 1:

using System;
using System.Xml;
namespace XmlReader
{
class Reader
{
static void Main(string[] args)
{
// Bir XmlTextReader yaratıp, ve Read methodunu çağırarak dosyayı okutacağız.
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();
// Node da bir değer varsa
while (textReader.Read())
{
// Birinci elemana geç.
textReader.MoveToElement();
Console.WriteLine("XmlTextReader Özellikler Test");
Console.WriteLine("<===================>");
// Bu elemanın özelliklerini oku ve Konsolda göster
Console.WriteLine("Isim:" + textReader.Name);
Console.WriteLine("Base URI:" + textReader.BaseURI);
Console.WriteLine("Yerel Isim:" + textReader.LocalName);
Console.WriteLine("Öznitelik Sayısı:" + textReader.AttributeCount.ToString());
Console.WriteLine("Derinlik:" + textReader.Depth.ToString());
Console.WriteLine("Satır Sayısı:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Türü:" + textReader.NodeType.ToString());
Console.WriteLine("Değer:" + textReader.Value.ToString());
}
}
}
}

Xml dosyalarının .NET Ortamında C# ile Okunup, Yazılması

Bu makale serisinde, Xml dosyalarının .NET ortamında C# diliyle nasıl okunup yazıldığını göreceksiniz.

Öncelikle Xml .NET Framework Kütüphaneleri ve isimalanlarıyla, sınıfları üzerine konuşacağız. Daha sonra Xml dosyası nasıl okunduğunu, nasıl yazıldığını göreceğiz. Son olarak makalenin sonunda ADO.NET ve XML.NET' in avantajlarını göreceğiz.

Microsoft .NET XML İsimalanları ve Sınıfları
.NET Framework' de Xml dosyası üzerinde çalışmaya başlamadan önce .NET Runtime Kütüphanesinin bize sağladığı .NET isimalanlarını ve sınıflarını bilmemiz çok önemlidir. .NET bize Xml sınıflarını desteklemek için beş isimalanı sağlamaktadır. -System.Xml, System.Xml.Schema, System.Xml.Serialization, System.Xml.XPath ve System.Xml.Xsl.
System.Xml isimalanı önemli Xml sınıflarını içerir. Bu isim alanı Xml dökümanlarını yazmak ve okumak için gerekli birçok sınıfı barıdırmaktadır. Bu makalede biz reader ve write sınıfları üzerine yoğunlaşacağız. Bu sınıflar - XmlReader, XmlTextReader, XmlValidatingReader, XmlNodeReader, XmlWriter ve XmlTextWriter. Gördüğünüz gibi dört okuma ve iki yazma sınıfı bulunmaktadır.

XmlReader sınıfı dökümanları okumak için gerekli methodları ve property' leri içermektedir. Read methodu bir streamdeki node' u okumak için kullanılır. Okuma fonksiyonu dışında, bu sınıf döküman içerisindeki nodeları gezmek içinde methodlar barındırmaktadır. Bu methodların bazıları MoveToAttribute, MoveToFirstAttribute, MoveToContent , MoveToFirstContent, MoveToElement ve MoveToNextAttribute. ReadString, ReadInnerXml, ReadOuterXml, ve ReadStartElement okuma methodlarıdır. Bu sınıf ayrıca skip current node (bu node u atla ) ve move to next one gibi (bir sonraki ne gec) gibi methodlara sahiptir. Bu methodları örneklerimizde göreceğiz.

XmlTextReader, XmlNodeReader ve XmlValidatingReader sınıfları XmlReader sınıfından gelmektedir. Adlarından da anlaşılabileceği gibi textleri,nodeları ve schema' ları okumak için kullanılırlar.

XmlWrite sınıfı Xml dokümanlarına data yazmakta kullanılır. Bu sınıf birçok yazma methodunu içerir, bunlarıda örneklerimizde göreceğiz.

XmlNode sınıfı önemli bir rol üslemektedir. Her nekadar bu sınıf Xml' in bir node' unu temsil etse de, temsil ettiği bu node Xml dokümmanının kok node' u olabilme özelliğini taşımaktadır. Bu temel sınıfların özeti olan bir sınıf gibidir ve bir cok node ekleme, çıkarma ve değiştirme sınıflarına sahiptir. XmlNode sınıfından türetilmiş 3 temel sınıf ise XmlDocument, XmlDataDocument ve XmlDocumentFragment' tir. XmlDocument bize dökümanları açmayı ve kaydetmeyi sağlar.

XmlDataDocument ise ADO.NET data set nesneleriyle çalışmayı sağlar.

System.Xml isimalanı da birçok sınıfı içermektedir. Bunların bazıları ise XmlConvert, XmlLinkedNode, ve XmlNodeList' dir.

Xml dökümanını okumak
Basit örneğimizde, Verileri ekrana göstermek için books.xml dosyasını kullanacağız. Bu dosya Vs.NET örnekleriyle(samples) birlikte gelmektedir. Bu dosyayı bulup "C:\books.xml" dizinine taşıyınız. Bu dosya genelde

"C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\LinqSamples\Data\books.xml" dizininde bulunur. İsterseniz başka bir xml dosyasıda kullanabilirsiniz.
Bir XmlTextReader instance' ı yarattıktan sonra Read methodunu çağırarak okumaya başlayacağız.

Visual Studio da File->New Project->Visual c#->Console Application Seçelim ve Adı XmlReader olsun.