Esempio connessione senza DataSource con MySql: web.config

FAQ di programmazione in .NET

Esempio connessione senza DataSource con MySql: web.config

Messaggioda alesslud » ven giu 16, 2006 10:03 am

<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="Path" value="Server=localhost;Database=abruzzoparchi;Uid=yourusername;Pwd=yourpassword;"/>
</appSettings>
<connectionStrings />
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<!--authentication mode="Windows"/-->
<customErrors mode="Off"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
Ultima modifica di alesslud il ven giu 16, 2006 10:20 am, modificato 1 volta in totale.
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Creiamo la tabella anagrafica

Messaggioda alesslud » ven giu 16, 2006 10:04 am

-- Struttura della tabella `anagrafica`
--

CREATE TABLE `anagrafica` (
`nome` varchar(50) NOT NULL default '',
`cognome` varchar(50) NOT NULL default '',
`indirizzo` varchar(150) NOT NULL default '',
`ID` int(11) NOT NULL auto_increment,
PRIMARY KEY (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

--
-- Dump dei dati per la tabella `anagrafica`
--

INSERT INTO `anagrafica` VALUES ('Alessandro', 'Ludovici', 'Via delle Nocelle', 1);
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Creiamo la classe per la connessione

Messaggioda alesslud » ven giu 16, 2006 10:06 am

using System;
using System.Data;
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 MySql.Data.MySqlClient;

/// <summary>
/// Summary description for ConnectionMysql
/// </summary>
public class ConnectionMysql
{
public ConnectionMysql()
{
//
// TODO: Add constructor logic here
//
}

public MySqlConnection getConnection()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.AppSettings["Path"]);
return conn;
}

}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Creiamo la pagina default2.aspx per visualizzare un DataGrid

Messaggioda alesslud » ven giu 16, 2006 10:07 am

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
&nbsp;&nbsp;<asp:GridView ID="griglia" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Default2.aspx.cs

Messaggioda alesslud » ven giu 16, 2006 10:07 am

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 MySql.Data.MySqlClient;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.creaTesto();
}

private void creaTesto()
{
ConnectionMysql myConn = new ConnectionMysql();
MySqlConnection conn = myConn.getConnection();
conn.Open();
MySqlCommand command = new MySqlCommand();
MySqlDataReader dataReader = null;

command.CommandText = "select * from anagrafica";
command.CommandType = CommandType.Text;
command.Connection = conn;
dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

griglia.DataSource = dataReader;

griglia.DataBind();


}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

AVVERTENZA!!!!!

Messaggioda alesslud » ven giu 16, 2006 10:08 am

Vi ricordo che in ogni caso è preferibile utilizzare l'area <connectionStrings /> per stabilire la connessione al db. A tal proposito è presente un articolo sul forum.
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am


Torna a .NET

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron