Hi
I have below code . If already exists it should not be added
C#
string constr = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO Category VALUES (@CategoryId,@CategoryName)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@CategoryId", data.id);
cmd.Parameters.AddWithValue("@CategoryName", data.snippet.title);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}Thanks

Amit MohantyRank #112 · 20K reputationPosted Apr 10, 2024, 1:34 AM EDT
You can modify your SQL query to check if the category already exists before attempting to insert it by using the IF NOT EXISTS clause.
string constr = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { string query = "IF NOT EXISTS (SELECT 1 FROM Category WHERE CategoryId = @CategoryId) " + "BEGIN " + " INSERT INTO Category VALUES (@CategoryId, @CategoryName) " + "END"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@CategoryId", data.id); cmd.Parameters.AddWithValue("@CategoryName", data.snippet.title); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 10, 2024, 12:59 AM EDT
Try below code:
string constr = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { con.Open(); // Check if the category already exists bool exists = false; string checkQuery = "SELECT COUNT(*) FROM Category WHERE CategoryId = @CategoryId OR CategoryName = @CategoryName"; using (SqlCommand checkCmd = new SqlCommand(checkQuery, con)) { checkCmd.Parameters.AddWithValue("@CategoryId", data.id); checkCmd.Parameters.AddWithValue("@CategoryName", data.snippet.title); exists = (int)checkCmd.ExecuteScalar() > 0; } if (!exists) { // Only insert if the category does not already exist string query = "INSERT INTO Category (CategoryId, CategoryName) VALUES (@CategoryId, @CategoryName)"; using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue("@CategoryId", data.id); cmd.Parameters.AddWithValue("@CategoryName", data.snippet.title); cmd.ExecuteNonQuery(); } } con.Close(); }Thanks
Sam HobbsRank #107 · 20.5K reputationPosted Apr 10, 2024, 3:51 PM EDT
When creating a table you can specify fields in tables as being unique. It is an option for fields in tables. You can alter the existing table to specify that specific fields must be unique.
The code provided here is good, it is good to avoid attempting to add a row that should not be added but if you never want duplicates of a field then the table should be created approriately.
Lalji DhameliyaRank #3,336 · 431 reputationPosted Apr 12, 2024, 8:48 AM EDT
You can use the
MERGEstatement in SQL Server to achieve this. Here's how you can modify your code to useMERGE:string constr = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { string query = @" MERGE INTO Category AS target USING (VALUES (@CategoryId, @CategoryName)) AS source (CategoryId, CategoryName) ON target.CategoryId = source.CategoryId WHEN NOT MATCHED THEN INSERT (CategoryId, CategoryName) VALUES (source.CategoryId, source.CategoryName);"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@CategoryId", data.id); cmd.Parameters.AddWithValue("@CategoryName", data.snippet.title); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }