基础-ADO插入数据后返回自增ID @@IDENTITY
在文件上传中,没上传一个文件都会插入一条数据信息,那么就要返回插入的数据的id,以便进行真实删除操作。
以下是ADO操作数据库的返回方法:
string sql = string.Format(@"insert into PaperAttachment (PaperID, FileTitle, FileName, ContentType, FileSize,admin_name, CookieID, PaperType,Subfolder,intime) values (0,'{0}','{0}','{1}',{2},'{3}','{4}',{5},'{6}','{7}') SELECT @@IDENTITY as Id" , fileTitle , extension , filesize , swfupuser , cookieId , paperType , subfolder , intime ); //ado操作数据 链接对象 SqlConnection coreDB = new SqlConnection(); //链接字符串 coreDB.ConnectionString = ConfigurationManager.AppSettings["MYSqlConn"]; //执行SQL语句 SqlCommand myCommand = new SqlCommand(sql, coreDB); coreDB.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); string result = "0"; while (myReader.Read()) { result = myReader["id"].ToString(); } myReader.Close(); coreDB.Close();