我希望主键作为自动生成值.... [英] I want Primary key as a auto generate value....

查看:75
本文介绍了我希望主键作为自动生成值....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI !!

我创建了一个名为LateFee的表和RctNo作为主键,我还写了一个函数Autoge并在按钮''(添加)clilk事件上调用它但是它抛出异常cast指定无效我应该怎么做才能生成自动生成的数字..

  public   void  Autogen()
{
#region Autogen

cmd = new SqlCommand( 从LateFee选择max(RctNo),con);
cmd.Connection = con;
int i = 0 ;
尝试
{
if (con.State == ConnectionState .Closed)
{
con.Open();
}
i =( int )cmd.ExecuteScalar();
i = i ++;
txtrctno.Text = i.ToString();
con.Close();
}
catch (InvalidCastException ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
#endregion

}



谢谢

解决方案

这是一个非常非常糟糕的主意。

即使它有效,从数据库中获取最高数字并且将其递增1并不能确保数字是唯一的 - 实际上,当您退出方法时它可能不是唯一的。请记住,SQL服务器 设计 在多用户环境中工作,因此您必须假设另一个或多个用户将在同一时间执行完全相同的代码。你永远不应该试图提前预测一个身份字段的下一个值 - 在你插入一个新行之后获得该行的值并且你很好,但事先得到它只是要求复杂的麻烦生产。



那么先生下一行的命令是什么?



你不是。您只有在创建后才能获得行的ID。简单的方法是执行插入并选择单个操作:

  INSERT   INTO  MyTable(UserName,UserRole) VALUES ' 我的名字' 666 ); 
SELECT @@ IDENTITY

因为这是作为单个执行的通过SQL语句,它将返回由表中的插入字符赋予Identity字段的值。


在表中是什么是RctNo列的数据类型。你可以试试这个

Corrct this Line。

cmd = new SqlCommand(select max(Cast(RctNo as Integer))来自LateFee,con);

HI!!
I create table named "LateFee" and RctNo as Primary key and also i write one function "Autoge" and call it on button''s (Add) clilk event but it throw exception "cast specified is not valid" what should i do for generating number as auto generate ..

public void Autogen()
       {
           #region Autogen

           cmd = new SqlCommand("select max(RctNo) from LateFee ",con);
           cmd.Connection = con;
           int i=0;
           try
           {
               if (con.State == ConnectionState.Closed)
               {
                   con.Open();
               }
              i =(int)cmd.ExecuteScalar();
               i = i++;
               txtrctno.Text =i.ToString();
               con.Close();
           }
           catch(InvalidCastException ex)
           {
               MessageBox.Show(ex.Message);
               con.Close();
           }
           #endregion

       }


thanks

解决方案

That is a very, very bad idea.
Even if it worked, getting the highest number from the database and incrementing it by one does not ensure that teh number is unique - indeed, it may not be unique by the time you exit the method. Remember that SQL server is designed to work in a multiuser environment, so you have to assume that another user or users will be doing the exact same code at the exact same time. You should never, ever try to predict the "next value" of an identity field in advance - get the value for the row after you have inserted a new row and you are fine, but getting it in advance is just asking for complicated trouble in production.

"Then Sir what will be the command to be execute for getting next row????"

You don''t. You only get the id of a row after you have created it. The easy way is to do the insert and select as a single operation:

INSERT INTO MyTable (UserName, UserRole) VALUES ('My Name', 666);
SELECT @@IDENTITY

Because this is executed as a single statement by SQL, it will return the value given to the Identity field by the insert.


in your table what is data type of "RctNo" column. You can try this
Corrct this Line.
cmd = new SqlCommand("select max(Cast(RctNo as Integer)) from LateFee ",con);


这篇关于我希望主键作为自动生成值....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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