Insert and update a datetime into SQL database

Strah Behry
private void ButtonOk_Click(object sender, EventArgs e)
    {
        if (txtWedstrijdSchemaID.Text == "")
        {
            //Insert
            string SQL;
            SQL = "Insert into Wedstrijdschema (Team1, Team2, Datum)";
            SQL += " values (";
            SQL += "" + txtTeam1.Text + ",";
            SQL += "" + txtTeam2.Text + ",";
            SQL += "" + Convert.ToDateTime(txtDatum.Text) + "";
            SQL += ")";

            clDatabase.ExecuteCommand(SQL);
            vulLv();
        }
        else
        {
            //Update
            string SQL;
            SQL = "Update Wedstrijdschema SET ";
            SQL += "Team1 = " + txtTeam1.Text + ",";
            SQL += "Team2 = " + txtTeam2.Text + ",";
            SQL += "Datum = " + Convert.ToDateTime(txtDatum.Text) + "";
            SQL += " where SchemaId = " + zoek;

            clDatabase.ExecuteCommand(SQL);
            vulLv();
        }
        txtDatum.Enabled = txtTeam2.Enabled = txtTeam1.Enabled = false;
    }

That is what I currently have, because of a trycatch it won't crash when I try, if I comment the txtDatum.Text out on the //insert and //upload it works (but obviously enters NULL for Datum in the Database) does anyone perhaps see where I'm going wrong?

EDIT: About the use of parameters, we need to use a threetier system where all SQL goes through a class which is the only one allowed to do anything with the database, this is how the command is executed:

public static bool ExecuteCommand(string SQLInstructie)
        {
            bool retour = true;
            SqlConnection Conn = new SqlConnection(clStam.Connstr);
            SqlCommand Cmd = new SqlCommand(SQLInstructie, Conn);

            try
            {
                Cmd.Connection.Open();
                Cmd.ExecuteNonQuery();
            }
            catch
            {
                retour = false;
            }
            finally
            {
                Conn.Close();
            }
            return retour;
        }

This works!! Thanks a lot for the help:

private void ButtonOk_Click(object sender, EventArgs e)
        {
            if (txtWedstrijdSchemaID.Text == "")
            {
                //Insert

                string SQL;
                SQL = "Insert into Wedstrijdschema (Team1, Team2, Datum)";
                SQL += " values (";
                SQL += "" + txtTeam1.Text + ",";
                SQL += "" + txtTeam2.Text + ",";
                SQL += "'" + Convert.ToDateTime(txtDatum.Text) + "'";
                SQL += ")";
                Debug.WriteLine(SQL);
                clDatabase.ExecuteCommand(SQL);
                vulLv();
            }
            else
            {
                //Update
                string SQL;
                SQL = "Update Wedstrijdschema SET ";
                SQL += "Team1 = " + txtTeam1.Text + ",";
                SQL += "Team2 = " + txtTeam2.Text + ",";
                SQL += "Datum = '" + Convert.ToDateTime(txtDatum.Text) + "'";
                SQL += " where SchemaId = " + zoek;

                clDatabase.ExecuteCommand(SQL);
                vulLv();
            }
            txtDatum.Enabled = txtTeam2.Enabled = txtTeam1.Enabled = false;
        }

EDIT: I'll promise to use parameterized SQL from now on!

Darren

You are missing a command , from the INSERT and UPDATE statement.

The syntax to insert data into the database is:

 INSERT INTO Table 
        (Column1, Column2, Column3) 
 VALUES
        ('Value 1', 'Value 2', 'Value3')

Aside that, you are vulnerable to SQL injection, use SQL paramerterised queries to prevent this.

I would first start off by using a SqlCommand object.

SqlCommand cmd = new SqlCommand("INSERT INTO Wedstrijdschema (Team1, Team2, Datum) VALUES (@V1, @V2, @V3");

cmd.Parameters.AddWithValue("@V1", txtTeam1.Text);
cmd.Parameters.AddWithValue("@V2", txtTeam2.Text);
cmd.Parameters.AddWithValue("@V3", Convert.ToDateTime(txtDatum.Text));

And then execute it using cmd.ExecuteNonQuery();

As an additional note I would also ensure that the value in txtDatum is converted correctly to the desired date format.

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

mySQL insert wont update database

分類Dev

Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB)

分類Dev

Sql query to insert datetime in SQL Server

分類Dev

Can't update then insert in SQL

分類Dev

Is there a non database-specific command for "insert or update"

分類Dev

How to store datetime with millisecond precision in SQL database

分類Dev

SQL-IF EXISTS UPDATE ELSE INSERT INTO

分類Dev

Send push notification to Android app after database insert or update

分類Dev

IF SQLでINSERTとUPDATEのための条件

分類Dev

Logical query processing phase of INSERT, DELETE, and UPDATE in SQL queries

分類Dev

SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

分類Dev

Insert or update data using cte_results in SQL Server

分類Dev

Update Temp table to Insert node in XML data using SQL

分類Dev

SQL Server:INSERT、UPDATE後にトリガー

分類Dev

MS Access Database insert values on a column using vba and sql query

分類Dev

How to INSERT INTO Azure SQL database from Azure Databricks in Python

分類Dev

Bulk insert into the SQL Server 2012 database - C#

分類Dev

how to update model after droping coulmn from sql database

分類Dev

Database not update when using apache camel sql component

分類Dev

Update latest "Datetime"

分類Dev

DateTime timezone not found in database

分類Dev

Realm order of insert or update

分類Dev

Insert POINT into postgres database

分類Dev

PHP MySQL Insert database

分類Dev

Insert image into MySQL database

分類Dev

Mysql Insert date in a database

分類Dev

Can I get Unix timestamp automatically converted to a DATETIME column when importing from CSV to a (My)SQL database?

分類Dev

Syntax for SQL Trigger to Insert Data in another DB and also to update any field in another db adfter a field is edited

分類Dev

VBA-SQL UPDATE / INSERT / SELECT to / fromExcelワークシート

Related 関連記事

  1. 1

    mySQL insert wont update database

  2. 2

    Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB)

  3. 3

    Sql query to insert datetime in SQL Server

  4. 4

    Can't update then insert in SQL

  5. 5

    Is there a non database-specific command for "insert or update"

  6. 6

    How to store datetime with millisecond precision in SQL database

  7. 7

    SQL-IF EXISTS UPDATE ELSE INSERT INTO

  8. 8

    Send push notification to Android app after database insert or update

  9. 9

    IF SQLでINSERTとUPDATEのための条件

  10. 10

    Logical query processing phase of INSERT, DELETE, and UPDATE in SQL queries

  11. 11

    SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

  12. 12

    Insert or update data using cte_results in SQL Server

  13. 13

    Update Temp table to Insert node in XML data using SQL

  14. 14

    SQL Server:INSERT、UPDATE後にトリガー

  15. 15

    MS Access Database insert values on a column using vba and sql query

  16. 16

    How to INSERT INTO Azure SQL database from Azure Databricks in Python

  17. 17

    Bulk insert into the SQL Server 2012 database - C#

  18. 18

    how to update model after droping coulmn from sql database

  19. 19

    Database not update when using apache camel sql component

  20. 20

    Update latest "Datetime"

  21. 21

    DateTime timezone not found in database

  22. 22

    Realm order of insert or update

  23. 23

    Insert POINT into postgres database

  24. 24

    PHP MySQL Insert database

  25. 25

    Insert image into MySQL database

  26. 26

    Mysql Insert date in a database

  27. 27

    Can I get Unix timestamp automatically converted to a DATETIME column when importing from CSV to a (My)SQL database?

  28. 28

    Syntax for SQL Trigger to Insert Data in another DB and also to update any field in another db adfter a field is edited

  29. 29

    VBA-SQL UPDATE / INSERT / SELECT to / fromExcelワークシート

ホットタグ

アーカイブ