Why is this thing not working?
[Database(Name="Relationships_Test")]
[Table(Name = "Order")]
public class Order
{
[Column(Name="ID", IsPrimaryKey=true)]
public int ID { get; set; }
[Column(Name = "OrderDate")]
public DateTime OrderDate { get; set; }
public void Save()
{
DataContext dc = new DataContext(@"Data Source=.\sqlexpress;Initial Catalog=Relationships_Test;Integrated Security=True");
dc.ExecuteCommand(@"INSERT INTO [Order] (ID,OrderDate)开发者_如何学Python VALUES (@ID,@OrderDate)", this.ID, this.OrderDate);
}
}
Order o = new Order();
o.ID = 3;
o.OrderDate = DateTime.Parse("12/31/2999");
o.Save();
This code generates an exception
Must declare the scalar variable "@ID".
You should write it like this
dc.ExecuteCommand(@"INSERT INTO [Order] (ID,OrderDate) VALUES ({0}, {1})", this.ID, this.OrderDate);
Check MSDN
It says
This method is a pass-through mechanism for cases where LINQ to SQL does not adequately provide for a particular scenario.
The syntax for the command is almost the same as the syntax used to create an ADO.NET DataCommand. The only difference is in how the parameters are specified. Specifically, you specify parameters by enclosing them in braces ({…}) and enumerate them starting from 0. The parameter is associated with the equally numbered object in the parameters array.
The following example opens a connection and passes a SQL UPDATE command to the SQL engine.
精彩评论