|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +// <Snippet1> |
| 4 | +using Microsoft.Data.SqlClient; |
| 5 | +using System.Data.SqlTypes; |
| 6 | + |
| 7 | +class Program |
| 8 | +{ |
| 9 | + static void Main() |
| 10 | + { |
| 11 | + string connectionString = GetConnectionString(); |
| 12 | + GetSqlTypesAW(connectionString); |
| 13 | + Console.ReadLine(); |
| 14 | + } |
| 15 | + static private void GetSqlTypesAW(string connectionString) |
| 16 | + { |
| 17 | + // Create a DataTable and specify a SqlType |
| 18 | + // for each column. |
| 19 | + DataTable table = new DataTable(); |
| 20 | + DataColumn icolumnolumn = |
| 21 | + table.Columns.Add("SalesOrderID", typeof(SqlInt32)); |
| 22 | + DataColumn priceColumn = |
| 23 | + table.Columns.Add("UnitPrice", typeof(SqlMoney)); |
| 24 | + DataColumn totalColumn = |
| 25 | + table.Columns.Add("LineTotal", typeof(SqlDecimal)); |
| 26 | + DataColumn columnModifiedDate = |
| 27 | + table.Columns.Add("ModifiedDate", typeof(SqlDateTime)); |
| 28 | + |
| 29 | + // Open a connection to SQL Server and fill the DataTable |
| 30 | + // with data from the Sales.SalesOrderDetail table |
| 31 | + // in the AdventureWorks sample database. |
| 32 | + using (SqlConnection connection = new SqlConnection(connectionString)) |
| 33 | + { |
| 34 | + string queryString = |
| 35 | + "SELECT TOP 5 SalesOrderID, UnitPrice, LineTotal, ModifiedDate " |
| 36 | + + "FROM Sales.SalesOrderDetail WHERE LineTotal < @LineTotal"; |
| 37 | + |
| 38 | + // Create the SqlCommand. |
| 39 | + SqlCommand command = new SqlCommand(queryString, connection); |
| 40 | + |
| 41 | + // Create the SqlParameter and assign a value. |
| 42 | + SqlParameter parameter = |
| 43 | + new SqlParameter("@LineTotal", SqlDbType.Decimal); |
| 44 | + parameter.Value = 1.5; |
| 45 | + command.Parameters.Add(parameter); |
| 46 | + |
| 47 | + // Open the connection and load the data. |
| 48 | + connection.Open(); |
| 49 | + SqlDataReader reader = |
| 50 | + command.ExecuteReader(CommandBehavior.CloseConnection); |
| 51 | + table.Load(reader); |
| 52 | + |
| 53 | + // Close the SqlDataReader. |
| 54 | + reader.Close(); |
| 55 | + } |
| 56 | + |
| 57 | + // Display the SqlType of each column. |
| 58 | + Console.WriteLine("Data Types:"); |
| 59 | + foreach (DataColumn column in table.Columns) |
| 60 | + { |
| 61 | + Console.WriteLine(" {0} -- {1}", |
| 62 | + column.ColumnName, column.DataType.UnderlyingSystemType); |
| 63 | + } |
| 64 | + |
| 65 | + // Display the value for each row. |
| 66 | + Console.WriteLine("Values:"); |
| 67 | + foreach (DataRow row in table.Rows) |
| 68 | + { |
| 69 | + Console.Write(" {0}, ", row["SalesOrderID"]); |
| 70 | + Console.Write(" {0}, ", row["UnitPrice"]); |
| 71 | + Console.Write(" {0}, ", row["LineTotal"]); |
| 72 | + Console.Write(" {0} ", row["ModifiedDate"]); |
| 73 | + Console.WriteLine(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static private string GetConnectionString() |
| 78 | + { |
| 79 | + // To avoid storing the connection string in your code, |
| 80 | + // you can retrieve it from a configuration file, using the |
| 81 | + // System.Configuration.ConfigurationSettings.AppSettings property |
| 82 | + return "Data Source=(local);Initial Catalog=AdventureWorks;" |
| 83 | + + "Integrated Security=SSPI;"; |
| 84 | + } |
| 85 | +} |
| 86 | +// </Snippet1> |
0 commit comments