vbdotnet.it

Tricks & tips, workaround, forum and ideas with .Net

About the author

Enrico Rossini è il gestore di questo blog.
E-mail me Send mail

Recent posts

Recent comments

Contributi

Best 6 ~ 6 users ~ 6 comments

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce. I marchi citati sono delle rispettive aziende.

© Copyright 2010

Advertising


Leggere\Scrivere un testo da un file di testo

Io non mi ricordo mai come leggere o scrivere un file di testo: ho ritenuto quindi di creare un post con due funzioni che possono essere utili.

   1: Imports System.IO
   2:  
   3: ' funzione di lettura del contenuto di un file
   4: Public Function GetFileContents(ByVal FullPath As String, _
   5:        Optional ByRef ErrInfo As String = "") As String
   6:         Dim strContents As String
   7:         Dim objReader As StreamReader
   8:         Try
   9:             objReader = New StreamReader(FullPath)
  10:             strContents = objReader.ReadToEnd()
  11:             objReader.Close()
  12:             Return strContents
  13:         Catch Ex As Exception
  14:             ErrInfo = Ex.Message
  15:         End Try
  16: End Function
  17:  
  18: ' funzione per il salvataggio di un testo in un file
  19: Public Function SaveTextToFile(ByVal strData As String, _
  20:      ByVal FullPath As String, _
  21:        Optional ByVal ErrInfo As String = "") As Boolean
  22:  
  23:         Dim Contents As String
  24:         Dim bAns As Boolean = False
  25:         Dim objReader As StreamWriter
  26:         Try
  27:             objReader = New StreamWriter(FullPath)
  28:             objReader.Write(strData)
  29:             objReader.Close()
  30:             bAns = True
  31:         Catch Ex As Exception
  32:             ErrInfo = Ex.Message
  33:         End Try
  34:         Return bAns
  35: End Function

 

Per poter utilizzare questo codice si può seguire il seguente esempio:

   1: sContents = GetFileContents("C:\test.txt", sErr)
   2: If sErr = "" Then
   3:    Debug.WriteLine("File Contents: " & sContents)
   4:    ' salva in un file differente
   5:    bAns = SaveTextToFile(sContents, "D:\Test.txt", sErr)
   6:    If bAns Then
   7:       Debug.WriteLine("File Saved!")
   8:    Else
   9:       Debug.WriteLine("Error Saving File: " & sErr)
  10:    End If
  11: Else
  12:    Debug.WriteLine("Error retrieving file: " & sErr)
  13: End If

Buon lavoro!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Monday, February 23, 2009 1:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Script SQL To Code

Ciao a tutti! A me capita che devo inserire nei miei programmi del codice SQL per la creazione di tabelle e non riesco mai a trovare un programma che mi genera un file Visual Basic.NET che mi piaccia. Allora mi lo sono creato. Semplice e fa quello che voglio: per ogni tabella crea una funzione con il nome della tabella e al termine mi crea una funzione AllDatabase che richiama tutte le tabelle. Ogni funzione fa riferimento alla funzione ExecuteCommand che può essere facilmente implementata anche seguento leindicazione che trovate in un altro mio post. Come fare per generare corretamente un file?

  1. Creare ad esempio da SQL Server lo script di generazione delle tabelle.
  2. Eliminare riferimento a SET ANSI_NULL e SET QUOTE_IDENTIFIER con i relativi GO
  3. Copiare lo script rimanente nel programma

Buon lavoro!

ScriptToCode.zip (10,33 kb)

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: sql | vb.net | windowsform
Posted by enrico on Monday, February 23, 2009 12:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Impostare una cultura in un progetto ASP.NET

Mi è capitato di installare un’applicazione su un server web e molti utenti esterni mi creavano dei problemi sulle date. Ho scoperto che alcuni utenti avevano il sistema operativo in Inglese e quindi le date venivano invertite ma non riconosciute dal server.

Ho risolto imponendo direttamente nel web.config le impostazioni sulla cultura in questo modo:

   1: <configuration>
   2:   <system.web>
   3:     <globalization culture="it-IT" uiCulture="it" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />
   4:   <system.web>
   5: </configuration>

Buon lavoro!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Wednesday, February 18, 2009 6:23 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Creare dinamicamente gli elementi di un ContextMenu

Non vi è mai capitato di aver la necessità di avere un menu contestuale che deve avere degli elementi che si devono creare dinamicamente?

Beh, io si e ho trovato alcune piccole difficoltà. Posto qui sotto il codice che consente di farlo. Inoltre c’è anche il controllo quando si clicca l’elemento del menu.

   1:  ' definisce l'elemento da inserire nel menu contestuale
   2:  Dim itm As New ToolStripMenuItem
   3:  itm.Tag = "Esempio"
   4:  itm.Text = "Esempio di testo"
   5:  AddHandler itm.Click, AddressOf Me.ToolStripItem_Click
   6:   
   7:  ' funzione di gestione del click
   8:  Private Sub ToolStripItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   9:          Dim tsi As ToolStripItem = CType(sender, ToolStripItem)
  10:          MsgBox(tsi.Text)
  11:  End Sub

A disposizione per info. Ciao a tutti!

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: vb.net | windowsform
Posted by enrico on Tuesday, February 10, 2009 10:42 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Elenco delle tabelle di un database in Microsoft SQL Server

Come ottenere direttamente da SQL Server l’elenco delle tabella presenti in un database? Avviando una nuova query sul database di interesse, scrivere il seguente script

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

Verrà restituito l’elenco delle tabelle presenti da porte utilizzare ad esempio con tablediff.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: sql | Tricks & Tips
Posted by enrico on Monday, February 02, 2009 12:26 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Differenza tra due tabelle in Microsoft SQL Server

A me capita spesso che eseguo delle modifiche al database sul mio PC di sviluppo e non mi scrivo quello che ho fatto. Divento così matto per trovare i campi che ho aggiunto.

Finalmente dalla versione 2005 di Microsoft SQL Server c’è un programmino veramente fico che consente di trovare le differenza tra due database sia a livello di struttura sia a livello di dati contenuti. In più crea lo script in linguaggio SQL per apportare le modifiche.

La sintassi generale è la seguente:

tablediff 
[ -? ] | 
{
        -sourceserver source_server_name[\instance_name]
        -sourcedatabase source_database
        -sourcetable source_table_name 
    [ -sourceschema source_schema_name ]
    [ -sourcepassword source_password ]
    [ -sourceuser source_login ]
    [ -sourcelocked ]
        -destinationserver destination_server_name[\instance_name]
        -destinationdatabase subscription_database 
        -destinationtable destination_table 
    [ -destinationschema destination_schema_name ]
    [ -destinationpassword destination_password ]
    [ -destinationuser destination_login ]
    [ -destinationlocked ]
    [ -b large_object_bytes ] 
    [ -bf number_of_statements ] 
    [ -c ] 
    [ -dt ] 
    [ -et table_name ] 
    [ -f [ file_name ] ] 
    [ -o output_file_name ] 
    [ -q ] 
    [ -rc number_of_retries ] 
    [ -ri retry_interval ] 
    [ -strict ]
    [ -t connection_timeouts ] 
}

La sintassi da eseguire da riga di comando per trovare la differenza tra due tabelle è la seguente:

tablediff -sourceserver serverorigine -sourcedatabase dborigine –sourcetable tabellaorigine 
   -destinationserver serverdestinazione -destinationdatabase dbdestinazione -destinationtable tabelladestinazione 
   -o diff_output.txt -F diff_script

Per ulteriori informazioni si veda il supporto Microsoft. Buon lavoro a tutti!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: sql | Tricks & Tips
Posted by enrico on Monday, February 02, 2009 12:20 PM
Permalink | Comments (1) | Post RSSRSS comment feed