Repository navigation
Expand file tree
/
Copy pathData.cpp
More file actions
59 lines (46 loc) · 1.8 KB
/
Copy pathData.cpp
File metadata and controls
59 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
#using "System.dll"
#using "System.Data.dll"
#using "System.Transactions.dll"
#using "System.EnterpriseServices.dll"
#using "System.Xml.dll"
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Data::SqlClient;
void main()
{
// Query user for user id and password
Console::Write("Enter User ID for Northwind Database :");
String ^userId = Console::ReadLine();
Console::Write("Enter password : ");
String ^password = Console::ReadLine();
// Connect to the SQL Database and issue a SELECT command all in one statement
String ^query = "SELECT * FROM Categories";
// build connect string with the userid and password
String ^connectString = String::Format("Data Source=localhost;Database=Northwind;UID={0};Password={1};", userId, password);
//You can use this format if you are connecting with a known user name and password,
// however it is not recommended to store these in the source for security reasons
//String ^ connectString = "Data Source=localhost;Database=Northwind;UID=sa;Password=";
SqlConnection^ sqlconn = gcnew SqlConnection(connectString);
sqlconn->Open();
SqlCommand ^sqlCommand = gcnew SqlCommand(query, sqlconn);
// Create a SqlDataReader to enumerate the result of executing the command against the database.
SqlDataReader ^dataReader = sqlCommand->ExecuteReader();
// Find number of columns in result
int numCols = dataReader->FieldCount;
// Display number of columns returned from query
Console::Write("No. of columns:");
Console::WriteLine(numCols);
// Display the data, separated by tabs
while(dataReader->Read())
{
for (int c = 0; c < numCols-1; c++)
{
Console::Write(dataReader[c]);
Console::Write("\t");
}
Console::WriteLine("");
}
}