How insert in sql server in c# through WPF
Means how can we insert,update delete in Sql server using wpf forms
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
dinesh kumarPosted Apr 22, 2015, 8:48 AM
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;
}