internal PlaniData(MySqlDataReader r) { StringBuilder sb = new StringBuilder(); Gala = r.GetUInt16(0); Sys = r.GetUInt16(1); Pla = r.GetUInt16(2); Planityp = r.GetString(3); Objekttyp = r.GetString(4); Ownername = r.GetString(5); Ownerally = r.IsDBNull(6) ? "" : r.GetString(6); Planiname = r.GetString(7); }
// 取 dr 中某字段的数据并返回 public static object GetValue( DbColumn col, MySqlDataReader r, int idx ) { switch( col.dataType ) { case DbDataTypes.Boolean: return r.GetBoolean( idx ); case DbDataTypes.Int8: return r.GetSByte( idx ); case DbDataTypes.Int16: return r.GetInt16( idx ); case DbDataTypes.Int32: return r.GetInt32( idx ); case DbDataTypes.Int64: return r.GetInt64( idx ); case DbDataTypes.UInt8: return r.GetByte( idx ); case DbDataTypes.UInt16: return r.GetUInt16( idx ); case DbDataTypes.UInt32: return r.GetUInt32( idx ); case DbDataTypes.UInt64: return r.GetUInt64( idx ); case DbDataTypes.Float: return r.GetFloat( idx ); case DbDataTypes.Double: return r.GetDouble( idx ); case DbDataTypes.DateTime: return r.GetDateTime( idx ); case DbDataTypes.String: return r.GetString( idx ); case DbDataTypes.Bytes: var len = (int)r.GetBytes( idx, 0, null, 0, 0 ); var buf = new byte[ len ]; r.GetBytes( idx, 0, buf, 0, len ); return buf; default: throw new Exception( "unsupported DbType" ); } }
//Searches ID number if it exist in the database //Old DB public Boolean SearchStudentID(int idNum) { student = new Student(); myConn = ConnectToDatabase(); //Makes a MySQL query string cmd = "SELECT student_id FROM coa.old_student_t WHERE student_id = @StudentID"; //Passes the command into the database connection MySqlCommand SelectCommand = new MySqlCommand(cmd, myConn); //To prevent SQL Injections, it would consider 'idNum' as a paramater SelectCommand.Parameters.AddWithValue("@StudentID", idNum); try { myConn.Open(); myReader = SelectCommand.ExecuteReader(); while (myReader.Read()) { try { student.StudentID = myReader.GetUInt16(0); } catch (InvalidCastException) { } } if (myReader.FieldCount > 0) { return true; } else { return false; } } catch (Exception) { return false; } }
// 往 dr 填充 r 于 i 索引的值 public static void FillValue( DbRow dr, MySqlDataReader r, int i ) { switch( dr.parent.columns[ i ].dataType ) { case DbDataTypes.Boolean: dr[ i ].Assign( r.GetBoolean( i ) ); break; case DbDataTypes.Int8: dr[ i ].Assign( r.GetSByte( i ) ); break; case DbDataTypes.Int16: dr[ i ].Assign( r.GetInt16( i ) ); break; case DbDataTypes.Int32: dr[ i ].Assign( r.GetInt32( i ) ); break; case DbDataTypes.Int64: dr[ i ].Assign( r.GetInt64( i ) ); break; case DbDataTypes.UInt8: dr[ i ].Assign( r.GetByte( i ) ); break; case DbDataTypes.UInt16: dr[ i ].Assign( r.GetUInt16( i ) ); break; case DbDataTypes.UInt32: dr[ i ].Assign( r.GetUInt32( i ) ); break; case DbDataTypes.UInt64: dr[ i ].Assign( r.GetUInt64( i ) ); break; case DbDataTypes.Float: dr[ i ].Assign( r.GetFloat( i ) ); break; case DbDataTypes.Double: dr[ i ].Assign( r.GetDouble( i ) ); break; case DbDataTypes.DateTime: dr[ i ].Assign( r.GetDateTime( i ) ); break; case DbDataTypes.String: dr[ i ].Assign( r.GetString( i ) ); break; case DbDataTypes.Bytes: var len = (int)r.GetBytes( i, 0, null, 0, 0 ); var buf = new byte[ len ]; r.GetBytes( i, 0, buf, 0, len ); dr[ i ].Assign( buf ); break; default: break; } }