C# C使用linq如何在列中插入数据
我想使用linq在sql中用随机数填充一列?C# C使用linq如何在列中插入数据,c#,linq,C#,Linq,我想使用linq在sql中用随机数填充一列? 我如何才能做到这一点?鉴于缺乏详细信息,这可能是一个解决方案: Random random = new Random(DateTime.Now.Milliseconds); using (var context = new Data.Entities()) { var entries = context.YourTable; foreach (var entry in entries )
我如何才能做到这一点?鉴于缺乏详细信息,这可能是一个解决方案:
Random random = new Random(DateTime.Now.Milliseconds);
using (var context = new Data.Entities())
{
var entries = context.YourTable;
foreach (var entry in entries )
{
entry.YourColumn = random.Next(100);
}
context.SaveChanges();
}
您必须使用c随机类
Random rnd = new Random();
int val = rnd.Next(1,100);
然后使用linq创建要插入到相关表中的对象
YourObject obj= new YourObject
{
Objval = val,
CreateDate = DateTime.Now
// …
};
//将新对象添加到Orders集合
yourdb.YourObjects.InsertOnSubmit(obj);
然后将更改提交到数据库
yourdb.SubmitChanges();
我想通过编写适当的代码,编译和执行。它正在工作,但当我回到数据库时,我没有找到我插入的内容,我该怎么办?向我们展示你为DB insertionRandom rnd=new Random编写的代码;使用var context=new saherenties{var entries=context.Employees;条目{entry.Name=Convert.tostringrn.Next100;}context.SaveChanges;且与Name属性关联的列未更新?未引发任何错误?是,数据库中的列未更新,且未引发任何错误。