sql = string.Format(" insert into neirong(lindex,title,contents,creattime,readcount,Tag)values('{0}','{1}','{2}','{3}',{4},'{5}') ", lindex, title, contents, DateTime.Now.ToString("yyyy-MM-dd"), 0, tag);
ret = ExcuteSQL(sql);
return ret;[code=C#][/code]
怎样获刚插入数据的ID
public int add()
{
sql = string.Format(" insert into neirong(lindex,title,contents,creattime,readcount,Tag)values('{0}','{1}','{2}','{3}',{4},'{5}');select @@IDENTITY", lindex, title, contents, DateTime.Now.ToString("yyyy-MM-dd"), 0, tag);
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}} /// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(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;
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
你的意思是Return刚插入的ID吗
代码如下
public CheckRecentId(string sql_Table){
try
{
SqlCommand cmd = new SqlCommand("select Max(id)from "+sql_Table, cn);
cn.Open();
int id = -1;
//返回查询结果中的第一行第一列,是个object对象
id = Convert.ToInt32(cmd.ExecuteScalar()); if (count < 1)
return false;
return true;
}
catch (Exception ex)
{
return false;
}