从数据库中获得自动编号值 [英] get autoNumber value from database

查看:294
本文介绍了从数据库中获得自动编号值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码如下。在我的代码
我更新现有的行(从现有的表),该方案从文本框获取所有更新的值。
有一个在被命名为codonsAutoNum的表格自动编号字段的结束(最后一栏)

The code is below. in my code I updating existing row(from existing table) that the program get all the updated values from textBoxes. there is at the end(the last column)of the table autoNumber field which is named as "codonsAutoNum"

我不知道怎么弄的从databese值(自动编号值)。
我需要在位置的值(代码),那里是'??'。

I don't know how to get the value (autonumber value) from the databese. I need the value in the position (in the code) where there is '??'.

感谢大家的帮助!

中的代码:

        private void update_Click(object sender, EventArgs e)
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
        OleDbConnection myConnection = new OleDbConnection(connectionString);
        string myInsertQuery =(String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " + 
        "codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " + 
        "codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}'" + 
        " WHERE codonsAutoNum='{9}'",codon1.Text, codon3.Text ,triplet1.Text, triplet2.Text,
         triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
        fullName.Text,??));
        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();



编辑:它得到了真正confused.How我的代码suppoused到可似乎?
我在哪里需要把SELECT @@身份?我不明白为什么我不能只是采取的价值?
(我需要它作为指数所选行图所在的更新。它的工作很好,当我这个ID(下同),但它并没有让我改变的全名field..there是一个更好的解决方案比自动编号列?

it got really confused.How does my code is suppoused to be seemed? Where do i need to put the "SELECT @@identity"?and i don't understand why can't I just take the value? (i need it as index to the selected row to figure where the update is. it worked well when i id this(below) but it didn't allowed me to change the fullname field..there is a better solution than the autonumber column?

        private void update_Click(object sender, EventArgs e)
    {


        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";

        OleDbConnection myConnection = new OleDbConnection(connectionString);
        string myInsertQuery =(String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " + 
        "codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " + 
        "codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}'" + 
        " WHERE codonsFullName = {9}",codon1.Text, codon3.Text ,triplet1.Text, triplet2.Text,
         triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
        fullName.Text,this.name));
        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();
        this.tblCodonsTableAdapter.Fill(this.myProjectDataSet.tblCodons);



NAME =全名中保存的值,在每点击价值chages

name= saved value of fullname, in each clicking the value chages

多一个疑问句。我试图做同样的e.Row(我保存在表中的每个点击),我得到一个错误System.Data.OleDb.OleDbException(0X80004005):可以在指数caue重复的值,或在mainKey或关系

one more Ques. I tried to do the same with e.Row (which i saved in each clicking in the table) and i get an error "System.Data.OleDb.OleDbException (0x80004005): can caue a duplicated values on the index,or on the mainKey or in the relationship" ‏‏

推荐答案

看到这一点:

http://msdn.microsoft.com/en -us /库/ ks9f57t0(v = VS.80)的.aspx

看到标有检索的Microsoft Access自动编号值一节

See the section labeled "Retrieving Microsoft Access Autonumber Values"

排序版本

OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY",
            connection)

我不相信SCOPE_IDENTITY()在访问中存在

I do not believe SCOPE_IDENTITY() exist in access

这篇关于从数据库中获得自动编号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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