17-10-12, 05:24 PM
كاتب الموضوع : AhmedEssawy
لانشاء قاعده بيانات و التعامل معها من خلال الكود
لانشاء قاعده بيانات
لانشاء قاعده بيانات
كود :
[align=left][FONT=Courier New][color=blue][FONT=Courier New]string[/FONT][/color][FONT=Courier New] strSQL = [color=maroon]"CREATE DATABASE HowToDemo"[/color];[/FONT][/FONT][FONT=Courier New]
[color=blue][FONT=Courier New]string[/FONT][/color][FONT=Courier New]connectionstring = [color=maroon]"Server=localhost;DataBase=;Integrated Security=SSPI"[/color];[/FONT]
[color=teal][FONT=Courier New]SqlConnection[/FONT][/color][FONT=Courier New] northwindConnection = [color=blue]new[/color] [color=teal]SqlConnection[/color](connectionstring);[/FONT][/FONT][/align]
[FONT=Courier New]
[align=left][color=green][FONT=Courier New]// A SqlCommand object is used to execute the SQL commands.[/FONT][/color]
[color=teal][FONT=Courier New]SqlCommand[/FONT][/color][FONT=Courier New] cmd = [color=blue]new[/color] [color=teal]SqlCommand[/color](strSQL,northwindConnection);[/FONT]
[FONT=Courier New]northwindConnection.Open();[/FONT]
[FONT=Courier New]cmd.ExecuteNonQuery();[/FONT]
[FONT=Courier New]northwindConnection.Close();[/FONT][/align]
[/FONT]
لانشاء جدول في قاعده موجوده
كود :
[align=left][FONT=Courier New][color=blue][FONT=Courier New]string[/FONT][/color][FONT=Courier New] strSQL = [color=maroon]"CREATE TABLE NW_Seafood ("[/color] + [/FONT][/FONT][FONT=Courier New]
[FONT=Courier New][color=maroon]"ProductID Int NOT NULL,"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"ProductName NVarChar(40) NOT NULL,"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"QuantityPerUnit NVarChar(20) NOT NULL,"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"UnitPrice Money NOT NULL,"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"(ProductID))"[/color];[/FONT]
[color=blue][FONT=Courier New]string[/FONT][/color][FONT=Courier New]connectionstring = [color=maroon]"Server=localhost;DataBase=;Integrated Security=SSPI"[/color];[/FONT]
[color=teal][FONT=Courier New]SqlConnection[/FONT][/color][FONT=Courier New] northwindConnection = [color=blue]new[/color] [color=teal]SqlConnection[/color](connectionstring);[/FONT][/FONT][/align]
[FONT=Courier New]
[align=left][color=green][FONT=Courier New]// A SqlCommand object is used to execute the SQL commands.[/FONT][/color]
[color=teal][FONT=Courier New]SqlCommand[/FONT][/color][FONT=Courier New] cmd = [color=blue]new[/color] [color=teal]SqlCommand[/color](strSQL,northwindConnection);[/FONT]
[FONT=Courier New]northwindConnection.Open();[/FONT]
[FONT=Courier New]cmd.ExecuteNonQuery();[/FONT]
[FONT=Courier New]northwindConnection.Close();[/FONT][/align]
[/FONT]
لانشاء اجراء PROCEDURE في قاعده موجوده
كود :
[align=left][FONT=Courier New][color=teal][FONT=Courier New]SqlConnection[/FONT][/color][FONT=Courier New] northwindConnection = [color=blue]new[/color] [color=teal]SqlConnection[/color](connectionstring);[/FONT][/FONT][FONT=Courier New]
[FONT=Courier New]northwindConnection.Open();[/FONT]
[color=blue][FONT=Courier New]string[/FONT][/color][FONT=Courier New] strSQL = “” ; [/FONT]
[color=teal][FONT=Courier New]SqlCommand[/FONT][/color][FONT=Courier New] cmd = [color=blue]new[/color] [color=teal]SqlCommand[/color](strSQL, northwindConnection);[/FONT]
[FONT=Courier New]cmd.CommandText = [color=maroon]"CREATE PROCEDURE AddSeafood AS"[/color] + [color=teal]Environment[/color].NewLine + [/FONT]
[FONT=Courier New][color=maroon]"INSERT INTO NW_Seafood"[/color] + [color=teal]Environment[/color].NewLine + [/FONT]
[FONT=Courier New][color=maroon]"(ProductID, ProductName, QuantityPerUnit, UnitPrice)"[/color] + [/FONT]
[FONT=Courier New][color=maroon]"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice "[/color] + [/FONT]
[FONT=Courier New][color=maroon]"FROM Northwind.dbo.Products "[/color] + [/FONT]
[FONT=Courier New][color=maroon]"WHERE CategoryID = 8"[/color];[/FONT][/FONT][/align]
[FONT=Courier New]
[align=left][FONT=Courier New]cmd.ExecuteNonQuery();[/FONT]
[FONT=Courier New]northwindConnection.Close();[/FONT][/align]
[/FONT]