Hey everyone,

For logging purposes, I want to pass an entire row of my DB to a file. Is there an easy way to store a row of items into an array or something similar that I could pass to a function for parsing?

e.g., array = SELECT * FROM table WHERE id=001
Then pass array to some function so that it can write the items to a log file.

maybe this will help - i have:

this.vehiclesTableAdapter.GetDataByID(ID_Number);

but i cant save it to an array

use SQL String commands in

using System.Data.SqlClient;

Then do something like

YourConn.Open();
SQLDataReader Sdr = new SQLCommand("SELECT * FROM table WHERE id=001",YourConn).ExecuteReader();
if(Sdr.Read())
{
   array[0] = sdr.GetInt32(0);
   array[1] = sdr.GetString(1);
   array[2] = //etc....
   
}
YourConn.Close();

Is there a way to get an array of items that aren't of the same datatype?

Try this.

DataTable dt = this.vehiclesTableAdapter.GetDataByID(ID_Number);
dt.Rows[0].ItemArray; //<--- Returns object array of row values

Let array be of type string.

YourConn.Open();
SQLDataReader Sdr = new SQLCommand("SELECT * FROM table WHERE id=001",YourConn).ExecuteReader();

if(Sdr.Read())
{
   array[0] = sdr.GetInt32(0).toString();
   array[1] = sdr.GetString(1);
   array[2] = //etc....
   
}
YourConn.Close();

since you will be saving it a txt file you won't need to conserve the data types. If you want to restore the DataType later you can just do

int.Parse(array[0])

I don't like using datagrids as execution time suffers but nick's suggestion will also work.

since you will be saving it a txt file you won't need to conserve the data types.

No mention of text file in OP.

I don't like using datagrids as execution time suffers

I used a DataTable. No DataGrid anywhere.
And Duki is already using the strongly typed DataTableAdapter class.

No mention of text file in OP.

Then pass array to some function so that it can write the items to a log file.

Ok granted didn't mention txt file, I assumed that much.

I used a DataTable. No DataGrid anywhere.
And Duki is already using the strongly typed DataTableAdapter class.

Sorry typo on my part there

Peace.

For logging purposes, I want to pass an entire row of my DB to a file

In this case you only concern with one row and use the Adapter Class. So using the Adapter fill data in to DataSet and using that youcan get that data row and easily write to a file.If you want DataRow can be passed as the parameter to a function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.