2 Replies - 1527 Views - Last Post: 29 April 2016 - 04:31 PM

#1 doe123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-April 16

Export a sql database table to a text file using vc++

Posted 28 April 2016 - 05:58 PM

I am trying to export a SQL database table to a text file. I can create the file but its blank. My issue is at the for each(DataColumn^ col in dt -> Rows->ToString()) line. Any help would be great.

SqlCommand^ myCommand = gcnew SqlCommand("SELECT * FROM [CSC 289].dbo.Inventory ;", myCon);
			  SqlDataReader^ myReader;

			  try
			  {
				  StreamWriter^ sw = gcnew StreamWriter("File.txt");

				  SqlDataAdapter^da = gcnew SqlDataAdapter();
				  da -> SelectCommand = myCommand;
				  DataTable^ dt = gcnew DataTable();

				  da -> Fill(dt); 

				  for each(DataRow^ row in dt -> Rows)
				  {
				 
					  for each(DataColumn^ col in dt -> Rows->ToString())
					  {
						  sw -> WriteLine(dt);
					  }
				  }

				  sw -> Close();
				  delete sw;



Is This A Good Question/Topic? 0
  • +

Replies To: Export a sql database table to a text file using vc++

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: Export a sql database table to a text file using vc++

Posted 28 April 2016 - 07:33 PM

Moving to C++ CLI.

The issue is that when you convert a the rows collection to a string, you can't get an columns out of a string, and so you never really enter the inner loop.

What you have to do is iterate over each row, and for each row, iterate over each column of the table, and then print out the value of each cell of the row that corresponds to that column.
Was This Post Helpful? 0
  • +
  • -

#3 doe123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-April 16

Re: Export a sql database table to a text file using vc++

Posted 29 April 2016 - 04:31 PM

I was able to figures it out. I just changed the line to this

sw -> Write(row[col->ColumnName]->ToString());

and not it works great.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1