1

I am searching for (PostId,UserId) into PostLikes table using SqlDataAdapter, if the row is found , I am using SqlCommandBuilder.GetDeleteCommand() to generate the delete instruction and deleting the underlying row, if the row is not found, then I use SqlCommandBuilder.GetInsertCommand() to generate the insert command and inserting the row to the table using SqlDataAdapter.Update(). But the row is not getting inserted to the table in database. Here is what I have done so far

SqlConnection con = new SqlConnection(connectionStrings);
SqlDataAdapter sqlDataAdapter=new SqlDataAdapter("select * from PostLikes where PostId like "
                                                 +postlike.PostId+" and UserId like "
                                                 +postlike.UserId,con);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds, "Result");
con.Open();
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
if(ds.Tables["Result"].Rows.Count==1)
{
    sqlDataAdapter.DeleteCommand = sqlCommandBuilder.GetDeleteCommand(true);
    msg = "Data is deleted";
}
else
{
    sqlDataAdapter.InsertCommand = sqlCommandBuilder.GetInsertCommand(true);
    msg = "Data is inserted";
}
sqlDataAdapter.Update(ds, "Result");

and the table
PostLikes(LikeId,PostId,UserId)

4

1 に答える 1