anche se non capisco l'uso di odbc, questo è un vecchio esercizio che scrive in un campo memo di Access e poi lo legge e lo mostra nello schermo
pagina aspx
codice:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ODBC aggiungo e leggo memo access.aspx.vb" Inherits="CorsoApogeo_wrox_gestione_dati_ODBC_aggiungo_e_leggo_memo_access" %>
<!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>Pagina senza titolo</title>
<link href="../../../stili/Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Scrivi e leggi Memo" />
<hr />
<div>
<asp:Literal ID="Literal1" runat="server" EnableViewState="false"></asp:Literal>
</div>
</div>
</form>
</body>
</html>
codice
codice:
Option Strict On
Partial Class CorsoApogeo_wrox_gestione_dati_ODBC_aggiungo_e_leggo_memo_access
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'leggo un file di circa 250.000 caratteri
Dim s As String = File.ReadAllText("C:\Documenti\Visual Studio 2005\Projects\libreria\libreria\ModuloWeb.vb")
Dim connessione As Odbc.OdbcConnection = Nothing
Dim comando As Odbc.OdbcCommand = Nothing
Dim transazione As Odbc.OdbcTransaction = Nothing
Dim sql As String = ""
Try
connessione = New Odbc.OdbcConnection(StringaConnessione)
connessione.Open()
transazione = connessione.BeginTransaction()
comando = connessione.CreateCommand()
comando.Transaction = transazione
sql = "UPDATE [TABELLA12] SET [MEMO] = ? WHERE [ID] = ? "
comando.CommandText = sql
comando.Parameters.Clear()
'la stringa immessa deve essere necessariamente tagliata, pena errore
comando.Parameters.Add("memo", Odbc.OdbcType.VarChar, 65535).Value = s.Substring(0, 65535)
comando.Parameters.Add("id", Odbc.OdbcType.SmallInt).Value = 1
comando.ExecuteNonQuery()
transazione.Commit()
'adesso leggo il memo e lo metto nel literal
sql = "SELECT [MEMO] FROM TABELLA12 WHERE [ID]=?"
comando.CommandText = sql
comando.Parameters.Clear()
comando.Parameters.Add("id", Odbc.OdbcType.SmallInt).Value = 1
Me.Literal1.Text = "<pre>" & comando.ExecuteScalar().ToString() & "</pre>"
Catch ex As Exception
Me.Literal1.Text = Server.HtmlEncode(ex.Message).Replace(vbNewLine, "
")
If (transazione IsNot Nothing AndAlso transazione.Connection IsNot Nothing) Then transazione.Rollback()
Finally
If connessione IsNot Nothing Then connessione.Close()
End Try
End Sub
Private Function StringaConnessione() As String
Return "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\dati\test\test.mdb;Uid=Admin;Pwd=;"
End Function
End Class