[IWE] C# ODBCReader questions

Chris Rathman iwe@warhead.org.uk
Fri, 12 Jan 2007 15:51:46 -0600


Don't have time to explain, but here's code that should have the effect 
you want (see the iterate over columns):

            System.Data.SqlClient.SqlConnection Db;
            System.Data.SqlClient.SqlDataAdapter Da;
            System.Data.DataSet Ds;
            System.Data.DataTable Dt;
            System.Data.SqlClient.SqlCommand Cmd;
            System.Data.SqlClient.SqlDataReader Rs;

            string connectionStr = "Server=.; Database=NorthWind; 
Integrated Security=sspi";
            string commandStr;

            // open up a database connection
            Db = new System.Data.SqlClient.SqlConnection(connectionStr);
            Db.Open();

            // create a table
            commandStr = "CREATE TABLE TestCSharp(" +
                         "   TestCD      CHAR(2)     NOT NULL PRIMARY 
KEY," +
                         "   Description VARCHAR(80) NOT NULL)";
            Cmd = new System.Data.SqlClient.SqlCommand(commandStr, Db);
            Cmd.CommandType = System.Data.CommandType.Text;
            Rs = Cmd.ExecuteReader();
            Rs.Close();

            // example using DataAdapter (sans SqlConnection)
            commandStr = "SELECT * FROM TestCSharp";
            Da = new System.Data.SqlClient.SqlDataAdapter(commandStr, 
connectionStr);
            Ds = new System.Data.DataSet();
            Da.Fill(Ds, "Test");
            Dt = Ds.Tables[0];
            // iterate over the columns
            foreach (System.Data.DataColumn col in Dt.Columns)
            {
                string s = col.ColumnName;
            }
            // iterate over the rows
            foreach (System.Data.DataRow row in Dt.Rows)
            {
                System.Console.WriteLine(row["TestCD"] + " " + 
row["Description"]);
            }

            // drop the table
            commandStr = "DROP TABLE TestCSharp";
            Cmd = new System.Data.SqlClient.SqlCommand(commandStr, Db);
            Cmd.CommandType = System.Data.CommandType.Text;
            Rs = Cmd.ExecuteReader();
            Rs.Close();

            // close the database connection
            Db.Close();


Chris.
Barry Roomberg wrote:
> On Friday 12 January 2007 11:52, Chris Rathman wrote:
>   
>> You access it either way.  Rs["Example"] or Rs[n].  Accessing by index
>> is faster since it doesn't have to do a dictionary search.
>>     
>
> You misunderstand.
> I don't KNOW the field names at that point.
> This is the result of a "select *".
> And my goal is to assign them the a hash, by field name.
> So, is there a way of getting the list of field names in it?
>
>   
>> Chris.
>>
>> Barry Roomberg wrote:
>>     
>>> Since I can get the count of fields, and I can loop throught that, is
>>> there any what, given a numeric index, to determine the name of the
>>> field? This would enable generaric setup / assign code.
>>>
>>> On Friday 12 January 2007 11:43, Chris Rathman wrote:
>>>       
>>>> I've used OleDbDataReader and SqlDataReader in the past, but I'm not
>>>> sure of the particular type for ODBC?  I put together some notes on C#
>>>> that might be useful (see the section on databases):
>>>>
>>>>     http://www.angelfire.com/tx4/cus/notes/csharp-syntax.html
>>>>
>>>> In general, I could have something like:
>>>>
>>>>     ...  bunch of code to set up Command....
>>>>
>>>>     OleDbDataReader Rs = Cmd.ExecuteReader();
>>>>     Rs.Read();
>>>>     while (Rs.Read())
>>>>     {
>>>>         string example = (string)Rs["Example"];
>>>>     }
>>>>
>>>> So, the answer is likely that you want to use the associative array that
>>>> corresponds to the data reader object.
>>>>
>>>> Chris.
>>>>
>>>> Barry Roomberg wrote:
>>>>         
>>>>> If I do a "select *" in an ODBC connection, and then have the reader
>>>>> pulling the data, how can I pull the assoicated field name from the
>>>>> reader array? _______________________________________________
>>>>> IWE mailing list
>>>>> IWE@warhead.org.uk
>>>>> http://lists.warhead.org.uk/mailman/listinfo/iwe
>>>>>           
>>>> _______________________________________________
>>>> IWE mailing list
>>>> IWE@warhead.org.uk
>>>> http://lists.warhead.org.uk/mailman/listinfo/iwe
>>>>         
>>> _______________________________________________
>>> IWE mailing list
>>> IWE@warhead.org.uk
>>> http://lists.warhead.org.uk/mailman/listinfo/iwe
>>>       
>> _______________________________________________
>> IWE mailing list
>> IWE@warhead.org.uk
>> http://lists.warhead.org.uk/mailman/listinfo/iwe
>>     
> _______________________________________________
> IWE mailing list
> IWE@warhead.org.uk
> http://lists.warhead.org.uk/mailman/listinfo/iwe
>
>