Está en la página 1de 3

I. i tng Sqlconnection [using System.Data.SqlClient ;] 1.

Kt ni theo c quyn h iu hnh C php 1 : Server = servername [ \InstanceName ]; Database = databasename; Integrated Security = SSPI; C php 2 : Server = servername[ \InstanceName ]; Database = databasename; Integrated Security = true; C php 3 : Server = servername [ \InstanceName ]; Initial Catolog = databasename; Integrated Security = true; Ch : Ti khon ng nhp phi c khai bo trong phn Login 2. Kt ni theo c quyn SQL Server C php 1: Server = Servername[ \InstanceName ]; Database = Databasename; Use ID = Username; Password = YourPasword; [Connection Timeout = second;] [port = portno;] [Persist Security Info = true;] Ch : Servername c th l : (local) hoc ( . ) hoc (a ch IP) . C php 2 : Vi dng Attachment v phin bn SQL Server 2005 (SQLEXPRESS) @Data Source = (local)\SQLEXPRESS; AttachDbFilename = <ng dn ti file Database>; Integrated Security = true; Use Instance = true; 3. Tp tin lu chui kt ni Ta c th s dng cc nh dng *.ini hoc *.txt lu chui kt ni hoc lu chui kt ni ngay trong class hay form. Tuy nhin khi lm vic vi .Net chng ta nn s dng nh dng *.config c h tr sn. C php 1: ?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="SqlServer" connectionString= " Server

= servername[ \InstanceName ]; Database = databasename; Integrated Security = true; " providerName ="System.Data.SqlClient"/> </connectionStrings></configuration> - Cch c ni dung chui kt ni theo c php 1 ta s dng phng thc connectionStrings ca lp connectionStringSettings thuc khng gian tn System.Configuration; * * II. i tng SQLCommand 1. Khai bo SqlCommand sqlCommand; 2. Khi to C 4 kim khi to khai bo khi to i tng ny *

+ sqlCommand = new SqlCommand(); + sqlCommand = new SqlCommand(string CommandText); + sqlCommand = new SqlCommand(string CommandText, SqlConnection sqlConnection); + sqlCommand = new SqlCommand(string CommandText, SqlConnection sqlConnection, SqlTrasaction sqlTransaction); 3. Cc thuc tnh + CommandText: Cho php khai bo mt chui pht biu SQL Server VD : String strSQL = Select * from TBLSinhvien; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; + CommandType Cho php ta chn mt trong 3 gi tr enum l : Text,TableDirect,StoredProcedure VD: String strSQL = spDanhsachSV; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL;am sqlCommand.CommandType = CommandType.StoredProcedure; Lu : khi th tc c tham s truyn vo th ta c th s dng i tng SqlParameterCollection hay SqlParameter + CommandTimeout Cho php khai bo thi gian ch thc thi pht biu SQL c tnh bng VD : String strSQL = spDanhsachSV; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.CommandTimeout = 30; + Connection Cho php ta khi to i tng sqlConnection m khng cn phi thng qua Constructor VD : String strSQL = spDanhsachSV; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; sqlCommand.Connection = sqlConnection; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.CommandTimeout = 30; 4. Phng thc + Phng thc ExecuteNonQuery Thc thi cc pht biu SQL, th tc ni ti, v n tr v s bn ghi oc thc thi VD : sqlCommand.Connection = sqlConnection; sqlConnnection.Open(); String strSQL = delete from TBLSinhvien where Masv = SV0000001; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; int records = sqlCommand.ExcuteNonQuery(); sqlConnection.Close(); sqlConnection.Dispose(); giy

+ Phng thc ExecuteScalar Phng thc ny thc thi pht biu SQL Server gi tr tr v l kiu i tng (object)

N thng c dng ly gi tr ca tng cc mu tin hay gi tr ca ct hay hng th nht

+ Phng thc ExecuteReader Khc vi hai phng thc trn , phng thc ny tr v tp cc gi tr ch c mt chiu v dng i tng sqlDataReader nm d tp d liu .

+ Phng thc ExcuteXmlReader Phng thc ny tng t nh phng thc ExecuteReader nhng n tr v tp d liu c nh dng XML v s dng i tng XMLReader nm d tp d liu .

5. Xy dng lp dng chung cho SQLServer Khi chng ta mun s dng cc phng thc trn chung trong c s d liu SQL Server , c th truyn vo tham s th chng ta s xy dng lp dng chung , gi s l Database v kt hp vi tng SqlConnection phn trn .

También podría gustarte