Linq to sql插入问题 [英] Linq to sql insertion issue

查看:75
本文介绍了Linq to sql插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,使用Visual Studio 2010在VS 2010中添加了基于服务的数据库...当我运行以下代码时,我的运行没有错误,但是退出应用程序后检查数据库时,数据库中未添加行...

但我在datagridview中看到了新记录...

我尝试了以下操作:

创建另一个数据库
创建另一个应用程序
尝试使用sqlconnection插入
在另一台机器上尝试过.

Hello, Using Visual studio 2010, added service based Database in VS 2010... When I run following code I have runs without an error but when I check my database after exiting application, row is not added in database...

but I see new record in datagridview...

I tried following:

Creating another database
creating another application
tried inserting with sqlconnection
tried on another machine.

dbDataContext db = new dbDataContext();

var newitem = new emp_detail();

// newitem.user_id= txteid.Text.Trim();

newitem.com_id = 1;

newitem.f_name= txtefname.Text.Trim();

newitem.l_name= txtelname.Text.Trim();

newitem.address= txteadd.Text.Trim();

newitem.position= txteposition.Text.Trim();

newitem.b_date= dtpebdate.Text.ToString();

newitem.h_date= dtpehdate.Text.ToString();

newitem.notes= txtenotes.Text.Trim();

newitem.d_wages= txtedailywages.Text.Trim();

newitem.h_phone= txtehphone.Text.Trim();

newitem.mob_no= txtemobno.Text.Trim();

newitem.bonus= txtebonus.Text.Trim();

newitem.e_fname= txtecfname.Text.Trim();

newitem.e_lname= txteclname.Text.Trim();

newitem.e_relationship= txtecrelnshp.Text.Trim();

newitem.e_phone= txtecphone.Text.Trim();

newitem.e_mobno= txtecmobno.Text.Trim();

newitem.ot_wages= txteotwages.Text.Trim();

db.emp_details.InsertOnSubmit(newitem);

db.SubmitChanges();

MessageBox.Show("Saved");

推荐答案

您必须在table上定义一个primary key才能使更新生效.
You must have a primary key defined on your table for the update to work.


亲爱的同事,

当添加诸如基于服务的数据库或精简版数据库之类的数据库时,在运行项目时,它们每次都由解决方案文件托管在bin文件夹中.因此他们不再指向解决方案文件中包含的数据库.他们开始指出在bin文件夹中托管的那个.

深吸一口气,然后执行以下操作,

将数据库放在其他位置,然后像在app.config中一样更新对此的所有先前引用,您就完成了.请参阅,记录将被插入/更新.

还要在代码中进行以下更正,

Dear fellow,

When you add such databases like service based databases or compact edition databases, upon running the project they are hosted with the solution file every time in the bin folder. so they are no more pointing to the db contained in your solution file. They start to point the one hosted in the bin folder.

Take a deep breath and do the following,

Place the database in some other place and update all the previous references to this one like in app.config and you are done. See, the records will be inserted/updated.

Also do the following corrections in the code,

dbDataContext db = new dbDataContext();
 
var newitem = new emp_detail();
 
// newitem.user_id= txteid.Text.Trim();

newitem.com_id = 1;
 
newitem.f_name= txtefname.Text.Trim();
 
newitem.l_name= txtelname.Text.Trim();
 
newitem.address= txteadd.Text.Trim();
 
newitem.position= txteposition.Text.Trim();
 
newitem.b_date= dtpebdate.Text.ToString();
 
newitem.h_date= dtpehdate.Text.ToString();
 
newitem.notes= txtenotes.Text.Trim();
 
newitem.d_wages= txtedailywages.Text.Trim();
 
newitem.h_phone= txtehphone.Text.Trim();
 
newitem.mob_no= txtemobno.Text.Trim();
 
newitem.bonus= txtebonus.Text.Trim();
 
newitem.e_fname= txtecfname.Text.Trim();
 
newitem.e_lname= txteclname.Text.Trim();
 
newitem.e_relationship= txtecrelnshp.Text.Trim();
 
newitem.e_phone= txtecphone.Text.Trim();
 
newitem.e_mobno= txtecmobno.Text.Trim();
 
newitem.ot_wages= txteotwages.Text.Trim();
 
db.AddToemp_details(newitem); // corrected
 
db.SubmitChanges(); // or db.SaveChanges();  if you are using entity framework
 
MessageBox.Show("Saved");




祝你好运!




Good luck!




将对象创建行更改为

Hi,

change your object creation line to

Emp_detail newitem = new Emp_detail();



我希望它也对您有用

最好的



I hope it works for you too

All the Best


这篇关于Linq to sql插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆