C#获取访问2010 Autonumber



嗨,我正在提交表格以访问,但要在提交后将访问分配的自动号码显示在文本框中。在我的下方,任何建议都很好!

string cmdstr = "Insert into  TaskPerformed(TaskType,OtherType,Analyst,DateCompleted)Values(@b,@c,@d,@e)";
string query2 = "Select @@IDENTITY";
OleDbConnection con1 = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con1);
OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY", con1);//
con1.Open();
cmd.CommandText = query2;
com.ExecuteNonQuery();
con1.Close();
label16.Text = cmdNewID.ToString();

看来您遇到的问题是因为您没有执行第二个命令...并且在使用连接之前要关闭该连接

using(OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY", con1))
{
    con1.Open();
    cmd.CommandText = query2;
    com.ExecuteNonQuery();
    label16.Text = cmdNewID.ExecuteScalar();
}

相关内容

  • 没有找到相关文章

最新更新