public int createId(string tablename,string fieldname) { int iId; SqlCommand cmm = new SqlCommand(); SqlDataAdapter sda = new SqlDataAdapter(); cmm.Connection = this.sqlConnection1; try { if(this.sqlConnection1.State == ConnectionState.Closed) sqlConnection1.Open(); cmm.CommandText="select cast(isnull(max("+fieldname.Trim()+"),0) as int) from "+tablename+""; iId=(int)cmm.ExecuteScalar()+1; return(iId); } catch(Exception err) { MessageBox.Show(err.Message); return(-1); } finally { cmm.Dispose(); sda.Dispose(); sqlConnection1.Close(); } }
cmm.CommandText="select cast(isnull(max("+fieldname.Trim()+"),0) as int) from "+tablename+""; iId=(int)cmm.ExecuteScalar()+1;改成這樣好像不可以,請指教,謝謝!! cmm.CommandText="Select max(ID) as int from tables"; iId=(int)cmm.ExecuteScalar()+1;
在数据库SQL Server2000 中的"表"->"属性"->"表的标示列"选中ID列即可。
还是以前的rs 好用吧?rs.update 之后还可以马上回返rs("id") 呵呵如果你一定要用sql command 那没法的,除非你加标志然后再select我个人在inst new record 都很少用command,但update和delete 还是很常用的没有了rs 现在用DataSet,哭ing ..
{
int iId;
SqlCommand cmm = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
cmm.Connection = this.sqlConnection1;
try
{
if(this.sqlConnection1.State == ConnectionState.Closed)
sqlConnection1.Open();
cmm.CommandText="select cast(isnull(max("+fieldname.Trim()+"),0) as int) from "+tablename+"";
iId=(int)cmm.ExecuteScalar()+1;
return(iId);
}
catch(Exception err)
{
MessageBox.Show(err.Message);
return(-1);
}
finally
{
cmm.Dispose();
sda.Dispose();
sqlConnection1.Close();
}
}
iId=(int)cmm.ExecuteScalar()+1;改成這樣好像不可以,請指教,謝謝!!
cmm.CommandText="Select max(ID) as int from tables";
iId=(int)cmm.ExecuteScalar()+1;