如何使用linq to SQL在ASP.NET中生成自动启动字段 [英] How to generate auto incriment field in ASP.NET using linq to SQL

查看:76
本文介绍了如何使用linq to SQL在ASP.NET中生成自动启动字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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();  

    }

推荐答案

你在这里有一些矛盾...

There is some contradiction in what you have here...
// "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...


我猜这个你的桌子:



主题



Nos Int(identity | autoincrement)
CLASSS varchar
主题 varchar
课程 varchar
章节 varchar






如果Nos字段是整数:



1-从实体主题更改数据类型,它看起来像一个bool,为int更改bool



2-更改:

I gues this your table:

Subject

Nos Int (identity|autoincrement)
CLASSS varchar
Subjects varchar
lesson varchar
chapter varchar



if the Nos Field is integer:

1- Change the datatype from the entity Subject, It looks like a bool, change bool for int

2- Change:
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中生成自动启动字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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