Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sql query syntax I want to save Albeant, for the column (date) and the type of data (date and time) the table name (persdat)
Posted
Updated 30-Jul-11 5:43am
v2
Comments
Manas Bhardwaj 30-Jul-11 11:44am    
not sure what your question is!

C#
static MySqlConnection connection;
        public void Add(Test test)
        {
            openConnection();
            string query = "INSERT INTO Math (dateTime,degree,testTime) VALUES('" + String.Format("{0:yyyy-M-d HH:mm:ss}", test.DateTime) + "','" + test.Degree + "','" + test.TestTime + "')";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            closeConnection();
        }
        private void openConnection()
        {
            try
            {
                connection = new MySqlConnection("SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";");
                connection.Open();
                Isconnected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Isconnected = false;
            }
        }
        private void closeConnection()
        {
            try
            {
                connection.Close();
                Isconnected = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

MySql.Data.dll[^]
 
Share this answer
 
Comments
Mycroft Holmes 30-Jul-11 20:41pm    
Please try and show the correct methods. Using formatted strings to insert dates is not correct and you should promote parameterised queries. Read this article http://www.codeproject.com/KB/database/SqlInjectionAttacks.aspx
 
Share this answer
 
Read this article, SQL Injection Attacks and Some Tips on How to Prevent Them[^], especially the part on parameterised queries. It will help with your problem and get you started on the right track.
 
Share this answer
 

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