第十二章SQL语言简介.ppt
文本预览下载声明
王有禮教授編著 透視ASP.NET-第12章 第十二章 SQL語言簡介 講授大綱: 新增資料庫 新增資料表 新增資料 修改資料 刪除資料 查詢資料 新增資料庫 用SQL command來建立資料庫的語法如下: Create Database 新資料庫名稱 連結字串的寫法,其中database的值要設定成master,即“database=master”。 範例程式 %@ Page Language=vb % %@ Import Namespace=System.Data % %@ Import Namespace=System.Data.SQLClient % script runat=server sub Page_Load(obj as object, e as eventArgs) dim Conn as new _ SQLConnection(server=localhost;uid=sa;pwd=;database=master) dim objCmd as new SQLCommand(Create Database CourseInformation, Conn) try objCmd.Connection.Open objCmd.ExecuteNonQuery Response.write(Successful.) catch ex as Exception Response.write(Error updating the database.) end try objCmd.Connection.Close end sub /script 新增資料表(1/2) 方法一: Create table 新資料表名稱 ( 欄位名稱一 資料型別(長度) 是否允許Null 欄位名稱二 資料型別(長度) 是否允許Null …) 方法二: Select 欄位名稱一, 欄位名稱二, … into 新資料表名稱 from 舊資料表名稱 where 條件式 新增資料表(2/2) %@ Page Language=VB % %@ Import Namespace=System.Data % %@ Import Namespace=System.Data.SQLClient % script runat=server sub Page_Load(obj as object, e as eventArgs) dim Conn as new SQLConnection _ (server=localhost;uid=sa;pwd=;database=CourseInformation) dim strSQL as string strSQL = Create table Students ( strSQL += StudentName varchar(16) Not Null, strSQL += StudentNo char(5) Primary Key, strSQL += Department varchar(16) Not Null, strSQL += Class char(4) Not Null) dim objCmd as new SQLCommand(strSQL, Conn) try objCmd.Connection.Open objCmd.ExecuteNonQuery Response.write(Successful.) catch ex as Exception Response.write(資料庫更改錯誤) end try objCmd.Connection.Close end sub /script SQL Server中的資料型態(1/2) SQL Server中的資料型態(2/2) Grades、Courses、Teachers的建立 複合attributes為主鍵的語法: Constraint 主鍵名稱 Primary Key (欄位名稱一, 欄位名稱二, …) Create table Grades ( StudentNo char(5) Not Null, CourseID char(4) Not Null, Scores decimal(9) N
显示全部