Ciao Rambco,
nel seguente esempio utilizziamo un database SQL Server Express 2005 che contiene una tabella chiamata "CLIENTI".
La tabella contiene tre campi:
1) CODICE di tipo int
2) COGNOME di tipo varchar
3) NOME di tipo varchar.
Come prima cosa, in modalità "Design", trasciniamo un controllo "Table" nella nostra pagina.
Passiamo alla vista "Source" e inseriamo il codice html per l'header della tabella:
codice:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server">
<asp:TableRow runat="server" Font-Bold="true" ForeColor="Black" BackColor="Silver">
<asp:TableHeaderCell>Codice</asp:TableHeaderCell>
<asp:TableHeaderCell>Cognome</asp:TableHeaderCell>
<asp:TableHeaderCell>Nome</asp:TableHeaderCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Nell'evento "Page_Load" della nostra pagina, creiamo dinamicamente le righe della tabella utilizzando un oggetto "SqlDataReader" per leggere i dati dal database:
codice:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConn;
SqlCommand myCmd;
SqlDataReader myReader;
string strConn = @"Server=NOMESERVER\SQLEXPRESS;";
strConn += "Database=NOMEDATABASE;";
strConn += "User ID=sa;";
strConn += "Password=MyPassword;";
strConn += "Trusted_Connection=False;";
myConn = new SqlConnection();
myConn.ConnectionString = strConn;
myCmd = new SqlCommand();
myCmd.CommandText = "SELECT CODICE, COGNOME, NOME FROM CLIENTI";
myCmd.CommandType = CommandType.Text;
myCmd.Connection = myConn;
myCmd.Connection.Open();
myReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
TableRow tr = new TableRow();
TableCell tcCodice = new TableCell();
tcCodice.Text = myReader["CODICE"].ToString();
tr.Cells.Add(tcCodice);
TableCell tcCognome = new TableCell();
tcCognome.Text = myReader["COGNOME"].ToString();
tr.Cells.Add(tcCognome);
TableCell tcNome = new TableCell();
tcNome.Text = myReader["NOME"].ToString();
tr.Cells.Add(tcNome);
Table1.Rows.Add(tr);
}
myReader.Close();
myCmd.Dispose();
myConn.Dispose();
}
}
Si inizia dichiarando e inizializzando gli oggetti "Connection", "Command" e "DataReader" :
codice:
SqlConnection myConn;
SqlCommand myCmd;
SqlDataReader myReader;
string strConn = @"Server=PCVINCENZO\SQLEXPRESS;";
strConn += "Database=ProvaSqlServer2005;";
strConn += "User ID=sa;";
strConn += "Password=Vincenzo11235813;";
strConn += "Trusted_Connection=False;";
myConn = new SqlConnection();
myConn.ConnectionString = strConn;
myCmd = new SqlCommand();
myCmd.CommandText = "SELECT CODICE, COGNOME, NOME FROM CLIENTI";
myCmd.CommandType = CommandType.Text;
myCmd.Connection = myConn;
apriamo la connessione e leggiamo i dati dalla tabella clienti:
codice:
myCmd.Connection.Open();
myReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
Come vedi il metodo "ExecuteReader" dell'oggetto Command, restituisce un oggetto DataReader. Il parametro "CommandBehavior.CloseConnection" fa si che la connessione venga chiusa automaticamente quando chiudiamo il DataReader.
infine, leggiamo i record uno alla volta e creiamo dinamicamente le righe per l'oggetto Table1:
codice:
while (myReader.Read())
{
TableRow tr = new TableRow();
TableCell tcCodice = new TableCell();
tcCodice.Text = myReader["CODICE"].ToString();
tr.Cells.Add(tcCodice);
TableCell tcCognome = new TableCell();
tcCognome.Text = myReader["COGNOME"].ToString();
tr.Cells.Add(tcCognome);
TableCell tcNome = new TableCell();
tcNome.Text = myReader["NOME"].ToString();
tr.Cells.Add(tcNome);
Table1.Rows.Add(tr);
}
myReader.Close();
Spero di aver soddisfatto la tua curiosità ma, debbo dire, tutto questo, in asp.net, non ha molto senso: avremmo potuto fare la stessa cosa, in modo più efficiente, utilizzando un oggetto GridView (che, tra l'altro, offre funzionalità avanzate come la paginazione, senza dover scrivere una sola riga di codice).
Nell'esempio abbiamo utilizzato il provider specifico per SQL Server e quindi gli oggetti Connection, Command e DataReader hanno il prefisso Sql.
Esistono altri tipi di provider. Per esempio, si può utilizzare OLE DB. In questo caso gli oggetti vanno dichiarati col prefisso "OleDb":
codice:
using System.Data.OleDb;
...
...
OleDbConnection myConn;
OleDbCommand myCmd;
OleDbDataReader myReader;
...
...