Why is the row not inserted in the database? WITH#

Last update

I think I found why this is happening. Every time I try to debug my application, a new database file is created by Visual Studio in the debug folder. This file is deleted when my application closes, so every change I make at runtime is deleted. I do not know how to change this, but I will publish it when I find out.

**

Edited to add additional information

**

I created a class in C # to handle database operations. I have a method for reading from a database that works fine. But when I try to insert lines, nothing happens. There is no error or exception, the code works the way it was supposed to, reaches the return true statement, but when I check the DB after, I have no new data. The database is empty, so it has no conflicts with unique fields. The id field is not auto-generated, so the problem will not be either.

Here is the code:

public bool saveCity(City c)
    {
        string query = "INSERT INTO [dbo].[cities] ([Id], [Name], [Latitude], [Longitude], [Radius], [LastUpdate])" +
            " VALUES (@Id, @Name, @Latitude, @Longitude, @Radius, @LastUpdate);";
        try
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("@Id", c.Id);
                    cmd.Parameters.AddWithValue("@Name", c.Name);
                    cmd.Parameters.AddWithValue("@Latitude", c.Latitude);
                    cmd.Parameters.AddWithValue("@Longitude", c.Longitude);
                    cmd.Parameters.AddWithValue("@Radius", c.Radius);
                    cmd.Parameters.AddWithValue("@LastUpdate", c.Update);
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        catch (Exception)
        {
            return false;
        }
    }

Connection is defined as a class field:

connString = Properties.Settings.Default.DBConnectionString;

I have no triggers in my database or anything that could delete my rows after insertion. At the moment, he has only one table, cities, which looks like this:

DB screen capture

City class code:

class City
    {
        public int Id;
        public string Name;
        public int Radius;
        public double Latitude;
        public double Longitude;
        public DateTime Update;

        public City()
        {
            Id = -1;
            Name = "";
            Radius = 0;
            Latitude = 0;
            Longitude = 0;
            Update = DateTime.Now;
        }


}

, # DB-, , , - , , .

+4
3

, . Visual Studio . - , :

# Visual Studio?

+1

@ . :

string query = @"INSERT INTO cities (Id, name, coord_x, coord_y, radius, updated_at)" + " VALUES (?id, ?name, ?lat, ?lng, ?rad, ?update)";

 cmd.Parameters.Add("?id", MySqlDbType.Int32).Value = c.Id      

...

,

0

Change doubleto decimal, specify the type of DB in Parameters.Addand change the syntax INSERT INTO ... VALUES (@id,...)to INSERT INTO ... SELECT @id,.... Perhaps one of them is a problem, but it's better to change everything.

0
source

All Articles