开发者

Inserting data doesn't replicate into database

开发者 https://www.devze.com 2023-04-08 10:46 出处:网络
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.

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");
}


You have to change the connection string to point to your other database which is used in the server Explorer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号