Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I'm currently creating a small application using Windows Forms and SQLite. After reading some tutorials I implemented this method for data retrieval:

C#
public DataTable GetDataTable(ref SQLiteDataAdapter adapter, string sql)
        {
            DataTable dt = new DataTable();
         
            // Connect to database.
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            // Create database adapter using specified query
            using (adapter = new SQLiteDataAdapter(sql, connection))
            // Create command builder to generate SQL update, insert and delete commands
            using (SQLiteCommandBuilder command = new SQLiteCommandBuilder(adapter))
            {
                // Populate datatable to return, using the database adapter                
                adapter.Fill(dt);
            }
            return dt;
        }


(As well as another GetDataTable which doesn't take an SQLiteDataAdapter as parameter)

I have three classes, let's call them UI, Link and Database. The UI does nothing but displaying the data and raising events upon user interaction. The Link creates the Database and a SQLiteDataAdapter, retrieves a data table through the method mentioned above, and binds it to a data grid view on the UI. The user cannot alter the table through the data grid view, but should do so through some text boxes. (does this make binding the table to the dgv obosolete?)

What's the best way to get the user input from the text boxes to the database, using the adapter? Or should I use DataReader and some Insert method instead of an adapter?

As of know, the UI exposes its controls through Get-methods. Is there a better solution?

C#
        private void Initialize()
        {
            // Subscribe to userInterface events
            userInterface.DataGridViewSelectionChanged += new EventHandler(userInterface_DataGridViewSelectionChanged);
            userInterface.NewClicked += new EventHandler(userInterface_NewClicked);
            userInterface.SaveClicked += new EventHandler(userInterface_SaveClicked);

            // Get dataGridView from userInterface and bind to database
            bindingSource = new BindingSource();
            bindingSource.DataSource = database.GetDataTable(ref adapter, "SELECT * FROM SomeTable");
            userInterface.GetDataGridView().DataSource = bindingSource;
        }  

        void userInterface_DataGridViewSelectionChanged(object sender, EventArgs e)
        {
            if (userInterface.GetDataGridView().SelectedRows.Count != 0)
            {
                DataGridViewRow row = userInterface.GetDataGridView().SelectedRows[0];
                userInterface.GetIDTextBox().Text = row.Cells["PrimaryKey].Value.ToString();
                userInterface.GetOtherIDTextBox().Text = row.Cells["ForeignKey"].Value.ToString();

                DataTable dt = database.GetDataTable("SELECT * from SomeTable WHERE ForeignKey=" + row.Cells["ForeignKey"].Value);
                userInterface.GetLastNameTextBox().Text = dt.Rows[0]["LastName"].ToString();
                userInterface.GetFirstNameTextBox().Text = dt.Rows[0]["FirstName"].ToString();
                userInterface.GetCompanyTextBox().Text = dt.Rows[0]["Company"].ToString();

            }            
        }

        void userInterface_NewClicked(object sender, EventArgs e)
        {
            // Get all text boxes and clear them
            // Let the UI take care of this by itself?                     
        }

void userInterface_SaveClicked(object sender, EventArgs e)
        {
            // Get text/data from all text boxes and insert (or update if editing table) into database
            // adapter.Update(...)?
        }


Cheers!
Posted
Comments
ZurdoDev 4-May-13 22:54pm    
DataReaders do not write data to the database, they only read. Use a SqlCommand object.
randyBobandy 5-May-13 7:27am    
I know. I meant using the DataReader to retrieve tables and some insert method to insert into the db instead of an adapter for both.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900