嗨,我正在提交表格以访问,但要在提交后将访问分配的自动号码显示在文本框中。在我的下方,任何建议都很好!
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();
}