Advertisement
Guest User

Untitled

a guest
May 12th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8.  
  9. namespace ConsoleApplication2
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.  
  16.  
  17.             // This example needs the
  18.             // System.Data.SqlClient library
  19.  
  20.             #region Building the connection string
  21.  
  22.             string Server = "localhost";
  23.             string Username = "sa";
  24.             string Password = "fA4%1%bgsa5A";
  25.             string Database = "msdb";
  26.  
  27.             string ConnectionString = "Data Source=" + Server + ";";
  28.             ConnectionString += "User ID=" + Username + ";";
  29.             ConnectionString += "Password=" + Password + ";";
  30.             ConnectionString += "Initial Catalog=" + Database;
  31.  
  32.             #endregion
  33.  
  34.  
  35.             #region Try to establish a connection to the database
  36.  
  37.             SqlConnection SQLConnection = new SqlConnection();
  38.  
  39.             try
  40.             {
  41.                 SQLConnection.ConnectionString = ConnectionString;
  42.                 SQLConnection.Open();
  43.  
  44.                 // You can get the server version
  45.                 // SQLConnection.ServerVersion
  46.             }
  47.             catch (Exception Ex)
  48.             {
  49.                 // Try to close the connection
  50.                 if (SQLConnection != null)
  51.                     SQLConnection.Dispose();
  52.  
  53.                 // Create a (useful) error message
  54.                 string ErrorMessage = "A error occurred while trying to connect to the server.";
  55.                 ErrorMessage += Environment.NewLine;
  56.                 ErrorMessage += Environment.NewLine;
  57.                 ErrorMessage += Ex.Message;
  58.  
  59.                 // Show error message (this = the parent Form object)
  60.                // MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  61.  
  62.                 // Stop here
  63.                 return;
  64.             }
  65.  
  66.             #endregion
  67.  
  68.  
  69.             #region Execute a SQL query
  70.  
  71.             string SQLStatement = "SELECT * FROM [msdb].[dbo].[backupset]";
  72.  
  73.             // Create a SqlDataAdapter to get the results as DataTable
  74.             SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);
  75.  
  76.             // Create a new DataTable
  77.             DataTable dtResult = new DataTable();
  78.  
  79.             // Fill the DataTable with the result of the SQL statement
  80.             SQLDataAdapter.Fill(dtResult);
  81.  
  82.             // Loop through all entries
  83.             foreach (DataRow drRow in dtResult.Rows)
  84.             {
  85.                 // Show a message box with the content of
  86.                 // the "Name" column
  87.                 Console.WriteLine(drRow["Name"].ToString());
  88.             }
  89.  
  90.             // We don't need the data adapter any more
  91.             SQLDataAdapter.Dispose();
  92.  
  93.             #endregion
  94.  
  95.  
  96.             #region Close the database link
  97.  
  98.             SQLConnection.Close();
  99.             SQLConnection.Dispose();
  100.  
  101.             #endregion
  102.  
  103.  
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement