c# access数据库,字段自动编号
c# access数据库,某字段自动编号,我想获取自动编号的最大值,要怎么做啊,求详细代码啊
------解决方案--------------------select max(自动编号字段)from table
------解决方案-------------------- //conStrSQL你改成你的access,我这里用的SQL2005
string conStrSQL = "Data Source=xx.xx.xx.xx;Initial Catalog=xxxxx;User ID=xx;Password=xxxx";
string strSQL = "select max(自动编号字段)from table";
conn = new SqlConnection(conStrSQL);
SqlCommand cmd = new SqlCommand(strSQL, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());
------解决方案-------------------- public User RetrieveTheLast()
{
User user = new User();
using (IDbConnection conn = Helper.OpenConnection(dataAccess, connectionString))
{
string sql = string.Format("select top 1* from [User] order by ID desc");
IDataReader reader = dataAccess.ExecuteReader(conn, CommandType.Text, sql, null);
if (reader.Read())
{
user = BuildUser(reader);
}
}
return user;
}