如何取的刚刚加入表中的记录的ID值啊?高手赐教
SQL语句为 :
string SqlSelecti = "insert into [MSG](msg_title,msg_content)values( ' " + TextBox1.Text + " ', ' " + TextBox2.Text + " ') ";
现在的方法是这样方法,该怎么改呢:
public int IBM_SQL(string SqlSelect)
{
string dbPath = System.Configuration.ConfigurationSettings.AppSettings[ "dbPath "];
string ACconnStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source= " + HttpContext.Current.Server.MapPath(dbPath);
OleDbConnection ACconn = new OleDbConnection(ACconnStr);
ACconn.Open();
OleDbCommand ACcomm = new OleDbCommand(SqlSelect, ACconn);
ii = ACcomm.ExecuteNonQuery();
ACconn.Close();
return ii;
}
------解决方案--------------------相同的贴子,发一个就行了...
在
http://community.csdn.net/Expert/topic/5322/5322371.xml?temp=.5073816
中我回复了一个了,
如果你的ID是整型的自增长类型,那么可以通过@@IDENTITY, SCOPE_IDENTITY及IDENT_CURRENT 函数
返回最后插入的标识值。
比如在的程序里,可以在SqlSelecti 中加上一个select @@IDENTITY以返回刚加入的ID值.
------解决方案--------------------自增字段在access好象只能写个语句来查询一下的。
下面是一个函数,你直接调用就可以。
/// <summary>
/// 获得最大ID
/// </summary>
/// <param name= "FieldName "> </param>
/// <param name= "TableName "> </param>
/// <returns> </returns>
public static int GetMaxID(string FieldName,string TableName)
{
string strsql = "select max( " + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name= "SQLString "> 计算查询结果语句 </param>
/// <returns> 查询结果(object) </returns>
public static object GetSingle(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(CONN_STRING))
{
using(OleDbCommand cmd = new OleDbCommand(SQLString,connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}