获取ID kolumn中的最高ID号并增加

时间:2015-10-07 10:57:51

标签: sql linq gridview ado.net dataset

您如何从DataSet中的ID列获取最高ID号?我正在使用ADO.NET(C#)。如果我这样做:

dataSet.Tables["Students"].Columns["ID"].AutoIncrement = true;

当我到达已经存在的ID时,应用程序将抛出异​​常。从一个“高数字”开始只是一个丑陋的黑客。那么如何正确安全地完成这项工作呢?

我在GridView中添加新行并将该数据保存到缓存中,然后从缓存返回到DataSet,然后使用DataAdapter方法“Update”将新数据发送回SQL Server。

1 个答案:

答案 0 :(得分:1)

这是一种方法,因为该值将从数据库中插入,因为它是 public static int Current_type=-1; case MOVIE_TRAILER_LOADER:{ if(data.moveToFirst()){ Current_type=0; movieAdapter.swapCursor(data); } break; } case MOVIE_REVIEW:{ if(data.moveToFirst()){ Current_type=1; movieAdapter.swapCursor(data); } break; } public class movie_detailsAdapter extends CursorAdapter{ private final static int TYPES_COUNT=2; private final static int TRAILER_TYPE=0; private final static int REVIEW_TYPE=1; public static class Review_ViewHolder{ public final TextView review_author; public final TextView review_content; public Review_ViewHolder(View view){ review_author=(TextView) view.findViewById(R.id.review_author); review_content=(TextView) view.findViewById(R.id.review_content); } } public static class TRAILER_ViewHolder{ public final TextView Trailer_name; public TRAILER_ViewHolder(View view){ Trailer_name=(TextView) view.findViewById(R.id.trailer_name); } } @Override public int getViewTypeCount() { return TYPES_COUNT; } @Override public int getItemViewType(int position) { return (Movie_Fragment.Current_type==0)?TRAILER_TYPE:REVIEW_TYPE; } public movie_detailsAdapter(Context context, Cursor c, int flags) { super(context, c, flags); } @Override public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { //why int ViewType=getItemViewType(cursor.getPosition()); int layid=-1; switch (ViewType){ case TRAILER_TYPE:{ layid=R.layout.trailer_item; break; } case REVIEW_TYPE:{ layid=R.layout.review_item; break; } } View view= LayoutInflater.from(context).inflate(layid,viewGroup,false); if(ViewType==TRAILER_TYPE){ TRAILER_ViewHolder tHolder=new TRAILER_ViewHolder(view); view.setTag(tHolder); } if(ViewType==REVIEW_TYPE){ Review_ViewHolder rHolder=new Review_ViewHolder(view); view.setTag(rHolder); } return view; } @Override public void bindView(View view, Context context, Cursor cursor) { int ViewType=getItemViewType(cursor.getPosition()); if(ViewType==TRAILER_TYPE) { TRAILER_ViewHolder mHolder=(TRAILER_ViewHolder) view.getTag(); mHolder.Trailer_name.setText(cursor.getString(cursor.getColumnIndex(dbContract.MOVIE_VIDEOS.OWM_COLUMN_TRAILER_NAME))); } else if((ViewType==REVIEW_TYPE)){ Log.i("sadsadsadsadsa",cursor.getString(2)); Review_ViewHolder mHolder=(Review_ViewHolder) view.getTag(); mHolder.review_author.setText(cursor.getString(cursor.getColumnIndex(dbContract.MOVIES_REVIEWS_TABLE.OWM_COLUMN_REVIEW_CONTENT))); } } 列,您指定的值并不重要。但我会使用负值来表示此行当前未添加到数据库中。

因此增加added-rows DataTable的负行计数值:

IDENTITY

碰撞是不可能的,因为这个ID值只是暂时的,并且在插入记录后将被替换为实际值。