[dba-VB] What to do, what to do?

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Fri Nov 13 11:22:31 CST 2009


John,

Yes, of course you can have multiple DataTable(s) in a DataSet - I have just
posted a very simple sample - one of a myriad possible combinations, the
simplest one probably. And of course you can have your DataSet(s) structure
created statically in VS designer.

Yes, I think your plan is OK.

You probably do not need a DataReader but if you'll find you need one - in
P.S. is a simple sample.

In fact Gustav knows DataSets better than I do - please address your
questions to him also and to everybody else here - I will not have time to
reply any more postings today...

Thank you.

--
Shamil

P.S. DataReader sample:

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace TestConsole.Samples
{
    public class SqlDataReaderTest
    {
        public static void Run()
        {
            string connectionString = "Data Source=HAMBURG\\SQL2005;" +
                "Initial Catalog=Northwind;User Id=sa;Password=N/A";
            using (SqlConnection connection =
                   new SqlConnection(connectionString))
            {
                connection.Open();

                // Create empty [NewShippers] dataset in memory
                string sql1 = "select * from [NewShippers] where(1=0)";
                SqlCommand command = new SqlCommand(sql1, connection);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet newDataSet = new DataSet("NewShippers");
                adapter.Fill(newDataSet);

                // Load source [Shippers] in memory
                string sql = "select * from [Shippers]";
                command = new SqlCommand(sql, connection);
                SqlDataReader dataReader = command.ExecuteReader();

                // Add loaded [Shippers] to [NewShippers] im memory
                Console.WriteLine("*** dataReader ***");
                while (dataReader.Read())
                {
                    object[] items = { dataReader[0], dataReader[1],
dataReader[2] };
                    newDataSet.Tables[0].Rows.Add(items);

                    Console.WriteLine("{0} {1} {2}", items[0], items[1],
items[2]);
                }
                dataReader.Close(); // ! always close DataReader
 
                // Insert [NewShippers] into db
                (new SqlCommand("delete from [NewShippers]",connection))
                    .ExecuteNonQuery();  
                SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                adapter.InsertCommand = builder.GetInsertCommand(true);
                adapter.Update(newDataSet.Tables[0]);

                // Check that [NewShippers] were inserted OK
                Console.WriteLine("*** newDataSet ***");
                newDataSet.Clear();  
                adapter = new SqlDataAdapter(
                    new SqlCommand("select * from
[NewShippers]",connection));
                adapter.Fill(newDataSet);   
                foreach (DataRow dataRow in newDataSet.Tables[0].Rows)
                {
                    Console.WriteLine("{0} {1} {2}",
                        dataRow[0], dataRow[1], dataRow[2]);
                }
            }
        }

    }
}



<<< snip to get through dba-VB robot >>>
 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4604 (20091113) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list