Passaggio all'ora solare 27 ottobre 2024 03:00 02:00 sposta indietro l'orologio di 1 ora (si dorme 1 ora in più)
L'esempio C# legge dal web.config la stringa di connessione e tramite un command con parametri e legge il valore del campo title (jn solo campo, un solo valore) che ha IDObject uguale a 10 (Parametro @ID).
Ritorna il valore nella variabile title.

C#

/*
 * using System.Data.SqlClient;
 * <appSettings>
 * <add key="connection.ConnectionString"
 *   value="data source=<server o ip>;initial catalog=<nome db>;user id=<utente>;password=<password>;"
 *   />
 * </appSettings>
 */
string strCnn = System.Configuration.ConfigurationSettings.AppSettings["connection.ConnectionString"];
SqlConnection cnn = new SqlConnection(strCnn);
SqlCommand cmd = cnn.CreateCommand();
//cmd.CommandText = "<nome store procedure>";
//cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SELECT [title]"
	+ " FROM [TbObjects] "
	+ " WHERE [IDObject] = @ID ";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
cmd.Parameters["@ID"].Value = 10;

cnn.Open();
string title = (string) cmd.ExecuteScalar();
cnn.Close();

cmd = null;
cnn.Close();
cnn = null;
Tags:
C#236 Database75 Esempi225 SQL90
Potrebbe interessarti anche: