How insert in sql server in c# through WPF
Means how can we insert,update delete in Sql server using wpf forms
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
dinesh kumar64 reputationPosted Apr 22, 2015, 4:48 AM EDT
public int InsertArticle(Data DataObject)
{
//Create the SQL Query for inserting an article
string sqlQuery = String.Format("Insert into DataObject (DataObject Title, DataObject Body , DataObjectPublishDate, DataObjectCategoryID) Values('{0}', '{1}', '{2}', {3} );"
+ "Select @@Identity", DataObject.Title, DataObject.Body, DataObject.PublishDate.ToString("yyyy-MM-dd"), DataObject.CategoryID);
//Create and open a connection to SQL Server
SqlConnection connection = new SqlConnection(DatabaseHelper.ConnectionString);
connection.Open();
//Create a Command object
SqlCommand command = new SqlCommand(sqlQuery, connection);
//Execute the command to SQL Server and return the newly created ID
int newDataObject = Convert.ToInt32((decimal)command.ExecuteScalar());
//Close and dispose
command.Dispose();
connection.Close();
connection.Dispose();
// Set return value
return newDataObject;
}