get autoNumber value from database

2020-04-13 11:54发布

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"

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 '??'.

thank you all for your help!

the code:

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

EDIT: 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= saved value of fullname, in each clicking the value chages

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" ‏‏

2条回答
Juvenile、少年°
2楼-- · 2020-04-13 12:32

see this:

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

See the section labeled "Retrieving Microsoft Access Autonumber Values"

sort version

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

I do not believe SCOPE_IDENTITY() exist in access

查看更多
We Are One
3楼-- · 2020-04-13 12:51

I'd suggest moving the query from inline sql to a stored procedure. Not so much to stop SQL injection but because you can set the stored procedure to return the value of the identity (autonumber) field its just created. simply have it return SCOPE_IDENTITY after running the sql.

all that is assuming you are using SQL server (I'm not familiar with other db's but would assume they have similar functionality)

see link for more info

http://msdn.microsoft.com/en-us/library/ks9f57t0.aspx

EDIT: WOW this was answered fast! apologies for duplicate answers

EDIT2: looks like access exposes a variable @@IDENTITY that contains the last used/created autonumber. so you should be able to run and inline query and follow it with another along the lines of 'SELECT @@IDENTITY AS prevID'

查看更多
登录 后发表回答