SQL Server for beginners and Best Practices
http://msdn.microsoft.com/vstudio/express/sql/learning/default.aspx
http://technet.microsoft.com/en-us/sqlserver/bb331794.aspx
| Novembre 2009 | ||||||||||
| L | M | M | J | V | S | D | ||||
| 1 | ||||||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | ||||
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | ||||
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | ||||
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | ||||
| 30 | ||||||||||
|
||||||||||
SQL Server for beginners and Best Practices
http://msdn.microsoft.com/vstudio/express/sql/learning/default.aspx
http://technet.microsoft.com/en-us/sqlserver/bb331794.aspx
|
private void
button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection oSqlConnection =
new System.Data.SqlClient.SqlConnection();
oSqlConnection.ConnectionString = @"Data Source=.;Initial
Catalog=ContactDB;Integrated Security=SSPI;Connect Timeout=30;";
System.Data.SqlClient.SqlCommand oSqlCommand =
new System.Data.SqlClient.SqlCommand("INSERT INTO [ContactType]([ContactTypeLibelle])
VALUES('test');SELECT SCOPE_IDENTITY(); ", oSqlConnection);
oSqlConnection.Open();
object nresult =
oSqlCommand.ExecuteScalar();
oSqlConnection.Close();
}
|
|
DECLARE @var uniqueidentifier;
SET @var = NEWID(); INSERT INTO MaTable(ChampA, ChampB) VALUES(@var, 'abcdef'); SELECT @var |
SQLCMD
http://msdn2.microsoft.com/fr-fr/library/ms162773.aspx
un didacticiel
http://msdn2.microsoft.com/fr-fr/library/ms170207.aspx
un petit e-learning gratuit
https://www.microsoftelearning.com/france/eLearning/courseContent.aspx?courseId=50441&lc=1036
|
SELECT name FROM sysobjects WHERE type='P'
|
|
public NET2CsSQL.BObject.StoredProcedureCollection
FindStordProcedures()
{
NET2CsSQL.BObject.StoredProcedureCollection
oStoredProcedureCollection;
System.Data.SqlClient.SqlCommand oSqlCommand;
System.Data.DataTable oDataTableStoredProcedures;
oDataTableStoredProcedures = new System.Data.DataTable();
oSqlCommand = new System.Data.SqlClient.SqlCommand();
oStoredProcedureCollection = new
NET2CsSQL.BObject.StoredProcedureCollection();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = System.Data.CommandType.Text;
oSqlCommand.CommandText = "SELECT name FROM
sysobjects WHERE type='P'";
System.Data.SqlClient.SqlDataReader oSqlDataReader =
oSqlCommand.ExecuteReader();
while (oSqlDataReader.Read())
{
NET2CsSQL.BObject.StoredProcedure
oStoredProcedure;
oStoredProcedure = new
NET2CsSQL.BObject.StoredProcedure();
oStoredProcedure.Name = oSqlDataReader["Name"].ToString();
oStoredProcedureCollection.Add(oStoredProcedure);
}
return oStoredProcedureCollection;
}
|
|
private System.Data.SqlClient.SqlConnection oSqlConnection;
private void CreateConnection(String
sConnectionString)
{
try
{
oSqlConnection = null;
oSqlConnection = new
System.Data.SqlClient.SqlConnection();
oSqlConnection.ConnectionString = sConnectionString;
oSqlConnection.Open();
}
catch (Exception
ex)
{
throw ex;
}
}
private void CloseConnection()
{
try
{
oSqlConnection.Close();
}
catch (Exception
ex)
{
throw ex;
}
}
|
1 - Microsoft SQL Server Database Publishing Wizard 1.0
générer rapidement le script SQL d'une base de données ou la publier
2 - >> NET2CsExportData
un peu de pub pour un petit outil bien sympathique (qui demanderait à prendre de l'ampleur) que j'ai posté sur CodeS-SourceS
(permet d'exporter rapidement les données et structure d'une base de données Access ou SQL Server ou fichier Xml ou Excel des uns aux autres, permet également de générer le script SQL et un rapport simple sur la base chargée)
http://www.csharpfr.com/codes/EXPORTER-VOS-DONNEES-SQL-SERVER-ACCES-XML-EXCEL_41784.aspx
3 SQL Server 2005 Books Online (February 2007)
une aide qui a l'air trés complête avec didacticiels sur les différents aspects de SQL Server 2005
> les outils et utiliaires SQL Server
> Analysis Services
> Data mining
> Integration Services
> Notification Services
> Reporting Services
ou plus simplement directement en ligne
|
create table ContactGroup
(
groupid int not null identity(1,1) primary key,
groupname char(200) null,
groupmembers xml null
)
|
|
insert into ContactGroup(groupname,groupmembers)
values ('Amis',
'
<contact>
<contactid>1</contactid>
<contactname>Romagny Jérôme</contactname>
<contactemail>romagny13@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>2</contactid>
<contactname>Bzllin Marie</contactname>
<contactemail>mb3@yahoo.fr</contactemail>
</contact>
');
|
|
update ContactGroup
set groupmembers.modify('
insert
<contact>
<contactid>3</contactid>
<contactname>Martin Pierre</contactname>
<contactemail>Martin@yahoo.fr</contactemail>
</contact>
after (contact)[2]')
|
|
update ContactGroup
set groupmembers='
<contact>
<contactid>1</contactid>
<contactname>Romagny Jérôme</contactname>
<contactemail>romagny13@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>2</contactid>
<contactname>Bellin Marie</contactname>
<contactemail>mb3@yahoo.fr</contactemail>
</contact>
'
where groupid=1;
|
|
update ContactGroup
set groupmembers.modify('replace value of (contact/contactname/@nb)[1] with "Bellin Marie"')
where groupmembers.exist('contact/contactname/@nb="Bzllin Marie"')=1
|
|
select * from ContactGroup for xml auto
|
|
<ContactGroupgroupid="1"groupname="Amis
">
<groupmembers>
<contact>
<contactid>1</contactid>
<contactname>Romagny Jérôme</contactname>
<contactemail>romagny13@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>2</contactid>
<contactname>Bellin Marie</contactname>
<contactemail>mb3@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>3</contactid>
<contactname>Martin Pierre</contactname>
<contactemail>Martin@yahoo.fr</contactemail>
</contact>
</groupmembers>
</ContactGroup>
|
|
select * from ContactGroup for xml raw
|
|
<rowgroupid="1"groupname="Amis
">
<groupmembers>
<contact>
<contactid>1</contactid>
<contactname>Romagny Jérôme</contactname>
<contactemail>romagny13@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>2</contactid>
<contactname>Bellin Marie</contactname>
<contactemail>mb3@yahoo.fr</contactemail>
</contact>
<contact>
<contactid>3</contactid>
<contactname>Martin Pierre</contactname>
<contactemail>Martin@yahoo.fr</contactemail>
</contact>
</groupmembers>
</row>
|
|
select groupmembers.query('//contactname')
from ContactGroup
|
|
select groupmembers.query(
'for $contactgroup in contact
return $contactgroup/contactname
')
from ContactGroup
|
|
<contactname>Romagny Jérôme</contactname>
<contactname>Bellin Marie</contactname>
|
|
select groupmembers.value('(contact/contactname)[1]','char(200)')
from ContactGroup
|
|
delete ContactGroup
where groupid=1;
|
|
create xml schema collection contactgroupschema as
'<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="contactgroup" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="contactgroup" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="contactid" type="xs:string" minOccurs="0"
/>
<xs:element name="contactname" type="xs:string" minOccurs="0"
/>
<xs:element name="contactemail" type="xs:string" minOccurs="0"
/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>'
|
|
create table ContactGroupWithType
(
groupid int not null identity(1,1) primary key,
groupname char(200) null,
groupmembers xml(content contactgroupschema)
)
|
Exporter une base de données à l’aide du Database Publishing Wizard (Technos-sources)
Récupérer les relations (clés étrangères) de bases de données Sql Server
La requête :
|
public List<ForeignKey>
RecupererForeignKeys(System.Data.SqlClient.SqlConnection oSqlConnection)
{
List<ForeignKey>
ListForeignKeys;
ListForeignKeys = new List<ForeignKey>();
System.Data.SqlClient.SqlCommand
oSqlCommandForeignKey;
oSqlCommandForeignKey = new
System.Data.SqlClient.SqlCommand();
oSqlCommandForeignKey.Connection = oSqlConnection;
oSqlCommandForeignKey.CommandType = System.Data.CommandType.Text;
oSqlCommandForeignKey.CommandText = "select o.name as
TABLEFILLE, fc.name as CLEFILLE,ro.name as TABLEMERE, c.name as CLEMERE from sysobjects o join sysforeignkeys fk on fk.fkeyid = o.id join sysobjects ro on ro.id = fk.rkeyid join
syscolumns c on c.id = ro.id and c.colid = fk.rkey join syscolumns fc on fc.id = o.id and fc.colid = fk.fkey";
System.Data.SqlClient.SqlDataReader oSqlDataReader =
oSqlCommandForeignKey.ExecuteReader();
while(oSqlDataReader.Read())
{
// ici je stocke les différents champs
recuperes dans une liste générique
ForeignKey oForeignKey;
oForeignKey = new ForeignKey();
oForeignKey.TABLEFILLE = oSqlDataReader[0].ToString();
oForeignKey.CLEFILLE = oSqlDataReader[1].ToString();
oForeignKey.TABLEMERE = oSqlDataReader[2].ToString();
oForeignKey.CLEMERE = oSqlDataReader[3].ToString();
ListForeignKeys.Add(oForeignKey);
}
oSqlDataReader.Close();
return ListForeignKeys;
}
|
|
CREATE PROCEDURE Procedure_test(@nom
char(100))
AS
BEGIN
select * from dbo.PARENT where nomparent=@nom
END
GO
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Procedures_Stockees_sql2005
{
public partial class Form1 : Form
{
System.Data.SqlClient.SqlConnection oSqlConnection;
System.Data.SqlClient.SqlDataReader oSqlDataReader;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 1 connection (connectionstring)
// 2 command (connection,commandtype,commandtext,+
parametres)
// 3 execution requete avec ouverture
connection(executereader,executenonquery,executenonscalar,fill)
// 4 affichage (parcourt enregistrements ou affectation
datasource + binding)
// fermeture datareader + connection si acces datareader(mode
connecte)
this.Ouvrir_Connection();
oSqlDataReader = this.retourner_liste_parents_du_nom(textBox1.Text);
this.Afficher_liste_parents(oSqlDataReader);
this.Fermer_datareader(oSqlDataReader);
this.Fermer_Connection();
}
///<summary>
/// But : executer de la command definie et retourner
un datareader rempli
///</summary>
///<param
name="sNom"></param>
///<returns></returns>
public System.Data.SqlClient.SqlDataReader
retourner_liste_parents_du_nom(string sNom)
{
System.Data.SqlClient.SqlCommand oSqlCommand;
System.Data.SqlClient.SqlParameter oSqlParameter;
System.Data.SqlClient.SqlDataReader
oSqlDataReader;
try
{
// Command
oSqlCommand = new
System.Data.SqlClient.SqlCommand();
oSqlCommand.Connection = oSqlConnection;
// si on avait plusieur sparametres
pour notre procedure stockee
//
oSqlCommand.Parameters.Add(oSqlParameter2); //etc.
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "Procedure_test";
// PARAMETRES
// ATTENTION LE(S) PARAMETRE(S)
DECLARES DANS LE CODE .NET ET SQL DE LA PROCEDURE STOCKEE DOIVENT PORTER EXACTEMENT LE MEME NOM
//>
////oSqlParameter = new
System.Data.SqlClient.SqlParameter("@nom", SqlDbType.Char, 100);
////oSqlParameter.Value =
sNom;
//> detaille
oSqlParameter = new
System.Data.SqlClient.SqlParameter();
oSqlParameter.ParameterName = "@nom";
oSqlParameter.SqlDbType = SqlDbType.Char;
oSqlParameter.Value = textBox1.Text;
oSqlCommand.Parameters.Add(oSqlParameter);
// Execution de la
command
oSqlDataReader = oSqlCommand.ExecuteReader();
}
catch (Exception
ex)
{
throw ex;
}
return oSqlDataReader;
}
///<summary>
/// But : Afficher la liste
correspondante
///</summary>
///<param
name="oSqlDataReader"></param>
public void
Afficher_liste_parents(System.Data.SqlClient.SqlDataReader oSqlDataReader)
{
while (oSqlDataReader.Read())
{
//> Par index
//listBox1.Items.Add("Nom : " +
oSqlDataReader[0] + " - Prenom : " + oSqlDataReader[1]);
//> Par le nom de
colonne
listBox1.Items.Add("Nom : " +
oSqlDataReader["nomparent"] + " - Prenom : " + oSqlDataReader["prenomparent"]);
}
this.Fermer_datareader(oSqlDataReader);
}
///<summary>
/// But : initialiser la chaine de connection et ouvre
la connection si celle ci est fermée
///</summary>
public void Ouvrir_Connection()
{
oSqlConnection = new System.Data.SqlClient.SqlConnection();
oSqlConnection.ConnectionString = @"Data
Source=.SQLEXPRESS;Initial Catalog=mestests;Integrated Security=True";
// ouverture de la connection
if (oSqlConnection.State == ConnectionState.Closed)
{
oSqlConnection.Open();
}
}
///<summary>
/// But : fermer la connection
///</summary>
public void Fermer_Connection()
{
oSqlConnection.Close();
}
///<summary>
/// But : Fermer le datareader
///</summary>
///<param
name="oSqlDataReader"></param>
public void
Fermer_datareader(System.Data.SqlClient.SqlDataReader oSqlDataReader)
{
oSqlDataReader.Close();
}
}
}
|