Update database table using Linq to Sql
Posted by Vin on Jun-20-2008
- First things first, table needs to have a primary key defined
- The dbml file’s table needs to reflect the Primary key, if not set the Primary Key flag of the column to true, and auto-sync property to “Never”
Code snippet - Update an Xml datatype column with an Xml file
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
string sqlConnection = @"Data Source=servername;Initial Catalog=dbname;Integrated Security=True";
string xmlPath = @"C:\MyPath\";
using (StreamReader reader = new StreamReader(xmlPath + "MyXmlFile.myxml"))
{
string scalarXml = reader.ReadToEnd();
MyDataDataContext db = new MyDataDataContext(sqlConnection);
MyFile myFile = db.MyFiles.Single(f => f.FileName == "MyXmlFileType");
myFile.XmlContent = XElement.Parse(scalarXml);
myFile.ModifiedOn = DateTime.Now;
db.SubmitChanges();
System.Diagnostics.Debug.WriteLine(db.Log);
}
}
Posted in: Software development


