Data insertion does not replicate in database

advertisements

I have a written a sample code to insert data into a table and it says successfully inserted, but when I see the data in database in the database explorer, table shows nothing.

When I try to retrieve the data, it prints those values. Still I don't find any records in Visual Studio server explorer.

I have tried to google this problem, and I found another database in debug folder which replicates those results.

When I delete that database, I'm stuck with a "connection failed" error.

this is sample code:

DomainId_connection = openConnection();

cmd = new SqlCeCommand("INSERT INTO Temp_Table_(Id,StartTime) values(@Id ,@StartTime)");

cmd.Parameters.Add("@Id", SqlDbType.Int, 4);
setDomainId_cmd.Parameters.Add("@StartTime", SqlDbType.DateTime, 8);

cmd.Parameters["@Id"].Value = Id;
cmd.Parameters["@StartTime"].Value = DateTime.Now;

cmd.Connection = setDomainId_connection;
if (cmd.ExecuteNonQuery() > 0)
{
    Console.WriteLine("Domain id stored");
}

closeConnection(setDomainId_connection);

Can someone help me making this work?


Don't share connection, use connection pooling, use using blocks, and many more:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = commandText;

    command.Parameters.Add("@Id", SqlDbType.Int, 4).Value = id;
    command.Parameters.Add("@StartTime", SqlDbType.DateTime, 8).Value =  DateTime.Now;

   if (!(cmd.ExecuteNonQuery() > 0))
       throw new Exception("Domain id was not stored");
}