问题描述
我在asp.net C#中尝试使用Linq to SQl进行数据插入。
我遇到了Primery键的问题,如何为此编写代码..请参阅帮助我..
我尝试过的事情:
i was trying Data insert using Linq to SQl in asp.net C#.
I got problem with Primery key, How to write code for this.. pls help me..
What I have tried:
protected void Button4_Click(object sender, EventArgs e)
{
linkdbDataContext DB = new linkdbDataContext();
subject TC = new subject();
// "NOS" is the primerykey and autoincriment field, define in Sql Server.
TC.Nos = true;
TC.CLASSS = txclass.Text;
TC.Subjects = txsubject.Text;
TC.lesson = txlesson.Text;
TC.chapter = txchapter.Text;
DB.subjects.InsertOnSubmit(TC);
DB.SubmitChanges();
}
推荐答案
// "NOS" is the primerykey and autoincriment field, define in Sql Server.
TC.Nos = true;
所以NOS是一个SID(如此数字)或它是布尔值(当你设置为'true'时)...
在任何情况下你都不需要也不能设置自动增量(SID)的值SQL中的字段,你必须把它留给SQL引擎...
设置所有其他字段并插入新记录,SQL会将下一个值分配给自动增量字段...
So either NOS is a SID (so numeric) or it is boolean (as you set it to 'true')...
In any case you need not and can not set the value for an auto-increment (SID) field in SQL, you have to left it to the SQL engine...
Set all the other field and insert you new record, SQL will assign the next value to the auto-increment field...
protected void Button4_Click(object sender, EventArgs e)
{
linkdbDataContext DB = new linkdbDataContext();
subject TC = new subject();
// "NOS" is the primerykey and autoincriment field, define in Sql Server.
// TC.Nos = true;
TC.Nos = 0 //Zero the autoincrement should work, if not try comment this line
TC.CLASSS = txclass.Text;
TC.Subjects = txsubject.Text;
TC.lesson = txlesson.Text;
TC.chapter = txchapter.Text;
DB.subjects.InsertOnSubmit(TC);
DB.SubmitChanges();
}
这篇关于如何使用linq to SQL在ASP.NET中生成自动启动字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!