How can i make my database method more future proof?

James VH

I am currently working on a new application with a database data transfer between two databases. I currently have a method written, but it looks like I can make this code way more compact. I read an article that used the Using Statement, but I'm not sure how I can implement this in my code. Furthermore, I would like to address that I have the following public declarations:

private IDbConnection ExportConnection = new OleDbConnection(Helper.CnnVal("ExportDatabase"));
    private IDbConnection SchoolConnection;
    private DataTable exportDb = new DataTable();
    private DataTable schoolDb = new DataTable();
    private OleDbCommandBuilder cb;
    private OleDbDataAdapter dataAdapterSchool;
    private OleDbDataAdapter dataAdapterExport;
    private OleDbCommand dbCommand;
    private string ZoekQuery { get; set; }
    private DataAccess db = new DataAccess();
    private string SchoolConnectionString;
    private string selectSchoolQuery;

This is the method:

public void InlezenClientGegevens()
{
    if ((bool)Checbox.IsChecked)
    {
        SchoolConnectionString = $@"I removed the string for privacy reasons";
        try
        {
            ExportConnection.Open();
            ZoekQuery = $"SELECT * FROM Client WHERE Cliëntnummer BETWEEN '{TxtVan.Text}' AND '{TxtTm.Text}'";
            dbCommand = new OleDbCommand(ZoekQuery, (OleDbConnection)ExportConnection);
            dataAdapterExport = new OleDbDataAdapter(ZoekQuery, (OleDbConnection)ExportConnection);
            dbCommand.ExecuteNonQuery();
            dataAdapterExport.AcceptChangesDuringFill = false;
            dataAdapterExport.Fill(exportDb);
            ExportConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        SchoolConnection = new OleDbConnection(SchoolConnectionString);
        try
        {
            SchoolConnection.Open();
            selectSchoolQuery = "SELECT * FROM Info";
            dbCommand = new OleDbCommand(selectSchoolQuery, (OleDbConnection)SchoolConnection);
            dataAdapterSchool = new OleDbDataAdapter(selectSchoolQuery, (OleDbConnection)SchoolConnection);
            dbCommand.ExecuteNonQuery();
            dataAdapterSchool.FillSchema(schoolDb, SchemaType.Source);
            dataAdapterSchool.Fill(schoolDb);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        try
        {
            cb = new OleDbCommandBuilder(dataAdapterSchool);
            cb.GetUpdateCommand();
            schoolDb.Merge(exportDb, false, MissingSchemaAction.Add);
            dataAdapterSchool.Update(schoolDb);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }
}

EDIT:

public void InlezenClientGegevens()
{
    if ((bool)Checkbox.IsChecked)
    {
        try
        {
            using (var connection = new OleDbConnection(Helper.CnnVal("ExportDatabase")))
            {
                var ZoekQuery = $"SELECT * FROM client WHERE Cliëntnummer BETWEEN '{TxtVan.Text}' AND '{TxtTm.Text}'";
                using (var dbcommand = new OleDbCommand(ZoekQuery, connection))
                {
                    using (var exportAdapter = new OleDbDataAdapter(ZoekQuery, connection))
                    {
                        exportAdapter.AcceptChangesDuringFill = false;
                        exportAdapter.Fill(exportDb);
                    }
                }
            }
            using (var connection = new OleDbConnection($"))
            {
                var ZoekQuery = "SELECT * FROM info";
                using (var dbcommand = new OleDbCommand(ZoekQuery, connection))
                {

                    using (var SchoolAdapter = new OleDbDataAdapter(ZoekQuery, connection))
                    {
                        SchoolAdapter.FillSchema(schoolDb, SchemaType.Source);
                        SchoolAdapter.Fill(schoolDb);
                        using (var cb = new OleDbCommandBuilder(SchoolAdapter))
                        {
                            cb.GetUpdateCommand();
                            schoolDb.Merge(exportDb, false, MissingSchemaAction.Add);
                            SchoolAdapter.Update(schoolDb);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }          
    }
}
Tim Schmelter

You dont need to open close the connection if you use DataAdappter.Fill. I would also use the using statement for everything implementing IDisposable(like for the OleDbConnection and OleDbCommand, not necessary for the DataTable). So you should create the connection in the method.

You should also use parameters for your queries instead of string concatenation to avoid sql-injection. You should also try-parse the input first, to see if it's really an int:

public void InlezenClientGegevens()
{
    bool validVan = int.TryParse(TxtVan.Text, out int van);
    bool validTm = int.TryParse(TxtTm.Text, out int tm);
    if (!validVan || !validTm)
    {
        // inform user
        return;
    }

    if (Checkbox.IsChecked)
    {
        try
        {
            using (OleDbConnection connection = new OleDbConnection(Helper.CnnVal("ExportDatabase")))
            {
                string zoekQuery = $"SELECT * FROM client WHERE Cliëntnummer BETWEEN ? AND ?";
                using (OleDbCommand dbCommand = new OleDbCommand(zoekQuery, connection))
                {
                    dbCommand.Parameters.Add(new OleDbParameter("Van", OleDbType.Integer).Value = van);
                    dbCommand.Parameters.Add(new OleDbParameter("Tm", OleDbType.Integer).Value = tm);
                    using (OleDbDataAdapter exportAdapter = new OleDbDataAdapter(dbCommand))
                    {
                        exportAdapter.Fill(exportDb);
                    }
                }
            }
            // ...
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I make my trie more efficient?

分類Dev

How can I configure future to download more files?

分類Dev

How can I make the button more visible?

分類Dev

Can I make this method more generic/smaller? (DRY)

分類Dev

How can I make a method stream agnostic?

分類Dev

How can I make Elixir mix test output more verbose?

分類Dev

How can I make Elixir mix test output more verbose?

分類Dev

How can I make a switch statement more object oriented?

分類Dev

How can I make a recursive search for longest node more efficient?

分類Dev

How can I make a code more efficient and shorter?

分類Dev

How can I make Emacs scroll horizontally more smoothly?

分類Dev

How can I make WiFi more stable on Ubuntu 15.10?

分類Dev

How can I rewrite this Kotlin filter method more succinctly?

分類Dev

How can I create more colors for my plot?

分類Dev

Can I use multiple method on a future builder?

分類Dev

How can I use interfaces to allow for more than 1 struct type to make code more useable?

分類Dev

How can I add another database in my laravel project in heroku

分類Dev

How can i make this method return to that variable in that loop

分類Dev

How can I make my glob work for cogs on Linux

分類Dev

How can I make a table inside my shiny box with RandomIcon()

分類Dev

How can I make a triangle drawable on the top left of my view?

分類Dev

How can I make my button call a rotation of GIFs?

分類Dev

How can I make my sprite not go crosswise in pygame?

分類Dev

How can I make my div's appear in a horizontal way?

分類Dev

How can I make the pixel value of the height in my code dynamic?

分類Dev

How can I make my bot delete its own message?

分類Dev

My Prestashop is slow - how can I make it faster?

分類Dev

How can I prevent my Build Method from looping?

分類Dev

How can pandas str.extract method returns more match from my list?

Related 関連記事

  1. 1

    How can I make my trie more efficient?

  2. 2

    How can I configure future to download more files?

  3. 3

    How can I make the button more visible?

  4. 4

    Can I make this method more generic/smaller? (DRY)

  5. 5

    How can I make a method stream agnostic?

  6. 6

    How can I make Elixir mix test output more verbose?

  7. 7

    How can I make Elixir mix test output more verbose?

  8. 8

    How can I make a switch statement more object oriented?

  9. 9

    How can I make a recursive search for longest node more efficient?

  10. 10

    How can I make a code more efficient and shorter?

  11. 11

    How can I make Emacs scroll horizontally more smoothly?

  12. 12

    How can I make WiFi more stable on Ubuntu 15.10?

  13. 13

    How can I rewrite this Kotlin filter method more succinctly?

  14. 14

    How can I create more colors for my plot?

  15. 15

    Can I use multiple method on a future builder?

  16. 16

    How can I use interfaces to allow for more than 1 struct type to make code more useable?

  17. 17

    How can I add another database in my laravel project in heroku

  18. 18

    How can i make this method return to that variable in that loop

  19. 19

    How can I make my glob work for cogs on Linux

  20. 20

    How can I make a table inside my shiny box with RandomIcon()

  21. 21

    How can I make a triangle drawable on the top left of my view?

  22. 22

    How can I make my button call a rotation of GIFs?

  23. 23

    How can I make my sprite not go crosswise in pygame?

  24. 24

    How can I make my div's appear in a horizontal way?

  25. 25

    How can I make the pixel value of the height in my code dynamic?

  26. 26

    How can I make my bot delete its own message?

  27. 27

    My Prestashop is slow - how can I make it faster?

  28. 28

    How can I prevent my Build Method from looping?

  29. 29

    How can pandas str.extract method returns more match from my list?

ホットタグ

アーカイブ